<!DOCTYPE HTML>
<html lang="zh-CN">
    

<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta name="keywords" content="GAN生成对抗网络Tutorial, UESTC CV DeepLearning MachineLearning 洪卫 sunhw Hongwei 计算机视觉 深度学习">
    <meta name="baidu-site-verification" content="fmlEuI34ir">
    <meta name="google-site-verification" content="KeoTn_OFy4ndJwXNmm2gMeQfPhd7alqE9vQDwI32KCY">
    <meta name="description" content="
本文转载于博客园，感觉写得比较清晰，保存一下供以后查看。

GAN生成式对抗网络（一）——原理生成式对抗网络（GAN, Generative Adversarial Networks ）是一种深度学习模型
GAN包括两个核心模块
1.生成">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>GAN生成对抗网络Tutorial | 洪卫の博客</title>
    <link rel="icon" type="image/jpeg" href="/favicon.jpg">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <!-- <link rel="stylesheet" type="text/css" href="/css/matery.css"> -->
    <!-- <link rel="stylesheet" type="text/css" href="/css/my.css"> -->
    <!-- <link rel="stylesheet" type="text/css" href="/libs/highlight/styles/atom-one-dark.css"> -->
    
    <!-- <style type="text/css">
        
            code[class*="language-"],
            pre[class*="language-"] {
                white-space: pre !important;
            }
        
    </style> -->

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
    <!-- 今日诗词 -->
    <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
    
    <!-- 百度统计 洪卫  shw2018 add 2019.09.13 -->
    <script>
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?28c8c0fcedebccbf1e2bfc8b43c2e7b5";
          var s = document.getElementsByTagName("script")[0]; 
          s.parentNode.insertBefore(hm, s);
        })();
    </script>        
        
    <!-- 加入百度自动推送JS代码，全站激活，洪卫  shw2018 add 2019.08.13  -->
    
        <script>
            (function(){
                var bp = document.createElement('script');
                var curProtocol = window.location.protocol.split(':')[0];
                if (curProtocol === 'https') {
                    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
                }
                else {
                    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
                }
                var s = document.getElementsByTagName("script")[0];
                s.parentNode.insertBefore(bp, s);
            })();
        </script>
    

<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>


    <body>

        <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    <!-- 去掉 header 栏的 logo显示  洪卫 shw2018 2019.09.15-->
                    <!-- 
                    <img src="/favicon.jpg" class="logo-img" alt="LOGO">
                     -->
                    <span class="logo-span">洪卫の博客</span>
                </a>
            </div>
            


<!-- <a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>首页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>归档</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/List" class="waves-effect waves-light">
            
            <i class="fa fa-heartbeat"></i>
            
            <span>清单</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/galleries" class="waves-effect waves-light">
            
            <i class="fa fa-photo"></i>
            
            <span>相册</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>关于我</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/contact" class="waves-effect waves-light">
            
            <i class="fa fa-envelope"></i>
            
            <span>留言板</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>友情链接</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/navi" class="waves-effect waves-light">
            
            <i class="fa fa-rocket"></i>
            
            <span>快捷导航</span>
        </a>
    </li>
    
    <li>
        <a href="#searchModal" class="modal-trigger waves-effect waves-light">
            <i id="searchIcon" class="fa fa-search" title="搜索"></i>
        </a>
    </li>
</ul> -->

<!-- 支持二级菜单特性 洪卫 shw2018 modify 2019.09.17  -->
<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right nav-menu">
    
      <li class="hide-on-med-and-down nav-item" >

        
            <a href="/" class="waves-effect waves-light">
              
                <i class="fa fa-home"></i>
              
              <span>首页</span>
            </a>

            
      </li>
    
      <li class="hide-on-med-and-down nav-item" >

        
              <a href="/archives" class="waves-effect waves-light">
                
                  <i class="fa fa-archive"></i>
                
                <span>归档</span>
                <i class="fa fa-chevron-down" aria-hidden="true"></i>
              </a>

            <ul class="sub-nav menus_item_child ">
              
                <li> 
                  <a href="/categories" >
                    
                      <i class="fa fa-bookmark" style="margin-top: -20px;"></i>
                    
                    <span>分类</span>
                  </a>
                </li>
              
                <li> 
                  <a href="/categories/技术" >
                    
                      <i class="fa fa-code" style="margin-top: -20px;"></i>
                    
                    <span>技术</span>
                  </a>
                </li>
              
                <li> 
                  <a href="/categories/生活" >
                    
                      <i class="fa fa-file-text-o" style="margin-top: -20px;"></i>
                    
                    <span>生活</span>
                  </a>
                </li>
              
                <li> 
                  <a href="/categories/随想" >
                    
                      <i class="fa fa-commenting-o" style="margin-top: -20px;"></i>
                    
                    <span>随想</span>
                  </a>
                </li>
              
                <li> 
                  <a href="/categories/资源" >
                    
                      <i class="fa fa-cloud-download" style="margin-top: -20px;"></i>
                    
                    <span>资源</span>
                  </a>
                </li>
               
            </ul>
          
      </li>
    
      <li class="hide-on-med-and-down nav-item" >

        
              <a href="/List" class="waves-effect waves-light">
                
                  <i class="fa fa-heartbeat"></i>
                
                <span>清单</span>
                <i class="fa fa-chevron-down" aria-hidden="true"></i>
              </a>

            <ul class="sub-nav menus_item_child ">
              
                <li> 
                  <a href="/List/music" >
                    
                      <i class="fa fa-music" style="margin-top: -20px;"></i>
                    
                    <span>音乐</span>
                  </a>
                </li>
              
                <li> 
                  <a href="/List/movies" >
                    
                      <i class="fa fa-film" style="margin-top: -20px;"></i>
                    
                    <span>视频</span>
                  </a>
                </li>
              
                <li> 
                  <a href="/books/" >
                    
                      <i class="fa fa-th-list faa-bounce" style="margin-top: -20px;"></i>
                    
                    <span>书单</span>
                  </a>
                </li>
              
                <li> 
                  <a href="/movies/" >
                    
                      <i class="fa fa-film faa-vertical" style="margin-top: -20px;"></i>
                    
                    <span>电影</span>
                  </a>
                </li>
               
            </ul>
          
      </li>
    
      <li class="hide-on-med-and-down nav-item" >

        
            <a href="/galleries" class="waves-effect waves-light">
              
                <i class="fa fa-photo"></i>
              
              <span>相册</span>
            </a>

            
      </li>
    
      <li class="hide-on-med-and-down nav-item" >

        
            <a href="/about" class="waves-effect waves-light">
              
                <i class="fa fa-user-circle-o"></i>
              
              <span>关于我</span>
            </a>

            
      </li>
    
      <li class="hide-on-med-and-down nav-item" >

        
            <a href="/contact" class="waves-effect waves-light">
              
                <i class="fa fa-envelope"></i>
              
              <span>留言板</span>
            </a>

            
      </li>
    
      <li class="hide-on-med-and-down nav-item" >

        
            <a href="/friends" class="waves-effect waves-light">
              
                <i class="fa fa-address-book"></i>
              
              <span>友情链接</span>
            </a>

            
      </li>
    
      <li class="hide-on-med-and-down nav-item" >

        
            <a href="/navi" class="waves-effect waves-light">
              
                <i class="fa fa-rocket"></i>
              
              <span>快捷导航</span>
            </a>

            
      </li>
    

    <li>
        <a href="#searchModal" class="modal-trigger waves-effect waves-light">
            <i id="searchIcon" class="fa fa-search" title="搜索"></i>
        </a>
    </li>

</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/favicon.jpg" class="logo-img circle responsive-img">
        
        <div class="logo-name">洪卫の博客</div>
        <div class="logo-desc">
            
            电子科技大学 | 电子与通信工程 | 计算机视觉
            
        </div>
    </div>

    

    <!-- <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                首页
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                归档
            </a>
        </li>
        
        <li>
            <a href="/List" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-heartbeat"></i>
                
                清单
            </a>
        </li>
        
        <li>
            <a href="/galleries" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-photo"></i>
                
                相册
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                关于我
            </a>
        </li>
        
        <li>
            <a href="/contact" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-envelope"></i>
                
                留言板
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                友情链接
            </a>
        </li>
        
        <li>
            <a href="/navi" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-rocket"></i>
                
                快捷导航
            </a>
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/shw2018/shw2018.github.io" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul> -->

<!-- 支持二级菜单特性 洪卫 shw2018 modify 2019.09.17  -->
<ul class="menu-list mobile-menu-list">
    
        <li class="m-nav-item">
                
                    <a href="/" class="waves-effect waves-light">
                        
                        <i class="fa fa-fw fa-home"></i>
                        
                        首页
                    </a>
              
            </li>
        
        <li class="m-nav-item">
                
                    <a href="javascript:;">
                            
                            <i class="fa fa-fw fa-archive"></i>
                            
                            归档
                            <span class="m-icon"><i class="fa fa-chevron-right"></i></span>
                    </a>
                <ul>
                  
                    <li> 
                      <a href="/categories" >
                           
                            <i class="fa fa-bookmark" style="left: 25px; position: absolute;"></i>
                       
                       <span>分类</span>
                      </a>
                    </li>
                  
                    <li> 
                      <a href="/categories/技术" >
                           
                            <i class="fa fa-code" style="left: 25px; position: absolute;"></i>
                       
                       <span>技术</span>
                      </a>
                    </li>
                  
                    <li> 
                      <a href="/categories/生活" >
                           
                            <i class="fa fa-file-text-o" style="left: 25px; position: absolute;"></i>
                       
                       <span>生活</span>
                      </a>
                    </li>
                  
                    <li> 
                      <a href="/categories/随想" >
                           
                            <i class="fa fa-commenting-o" style="left: 25px; position: absolute;"></i>
                       
                       <span>随想</span>
                      </a>
                    </li>
                  
                    <li> 
                      <a href="/categories/资源" >
                           
                            <i class="fa fa-cloud-download" style="left: 25px; position: absolute;"></i>
                       
                       <span>资源</span>
                      </a>
                    </li>
                   
                </ul>
              
            </li>
        
        <li class="m-nav-item">
                
                    <a href="javascript:;">
                            
                            <i class="fa fa-fw fa-heartbeat"></i>
                            
                            清单
                            <span class="m-icon"><i class="fa fa-chevron-right"></i></span>
                    </a>
                <ul>
                  
                    <li> 
                      <a href="/List/music" >
                           
                            <i class="fa fa-music" style="left: 25px; position: absolute;"></i>
                       
                       <span>音乐</span>
                      </a>
                    </li>
                  
                    <li> 
                      <a href="/List/movies" >
                           
                            <i class="fa fa-film" style="left: 25px; position: absolute;"></i>
                       
                       <span>视频</span>
                      </a>
                    </li>
                  
                    <li> 
                      <a href="/books/" >
                           
                            <i class="fa fa-th-list faa-bounce" style="left: 25px; position: absolute;"></i>
                       
                       <span>书单</span>
                      </a>
                    </li>
                  
                    <li> 
                      <a href="/movies/" >
                           
                            <i class="fa fa-film faa-vertical" style="left: 25px; position: absolute;"></i>
                       
                       <span>电影</span>
                      </a>
                    </li>
                   
                </ul>
              
            </li>
        
        <li class="m-nav-item">
                
                    <a href="/galleries" class="waves-effect waves-light">
                        
                        <i class="fa fa-fw fa-photo"></i>
                        
                        相册
                    </a>
              
            </li>
        
        <li class="m-nav-item">
                
                    <a href="/about" class="waves-effect waves-light">
                        
                        <i class="fa fa-fw fa-user-circle-o"></i>
                        
                        关于我
                    </a>
              
            </li>
        
        <li class="m-nav-item">
                
                    <a href="/contact" class="waves-effect waves-light">
                        
                        <i class="fa fa-fw fa-envelope"></i>
                        
                        留言板
                    </a>
              
            </li>
        
        <li class="m-nav-item">
                
                    <a href="/friends" class="waves-effect waves-light">
                        
                        <i class="fa fa-fw fa-address-book"></i>
                        
                        友情链接
                    </a>
              
            </li>
        
        <li class="m-nav-item">
                
                    <a href="/navi" class="waves-effect waves-light">
                        
                        <i class="fa fa-fw fa-rocket"></i>
                        
                        快捷导航
                    </a>
              
            </li>
        

        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/shw2018/shw2018.github.io" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>

</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/shw2018/shw2018.github.io" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

        



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/22.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        GAN生成对抗网络Tutorial
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px; /* 20px */
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/深度学习/" target="_blank">
                                <span class="chip bg-color">深度学习</span>
                            </a>
                        
                            <a href="/tags/CV/" target="_blank">
                                <span class="chip bg-color">CV</span>
                            </a>
                        
                            <a href="/tags/GAN/" target="_blank">
                                <span class="chip bg-color">GAN</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fa fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Reaserch/" class="post-category" target="_blank">
                                Reaserch
                            </a>
                        
                    </div>
                    
                </div>
            </div>
            
            <div class="post-info">
                <div class="post-date info-break-policy">
                    <i class="fa fa-calendar-minus-o fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-12-10
                </div>

                

                <div class="post-author info-break-policy">
                    <i class="fa fa-user-o fa-fw"></i>作者:&nbsp;&nbsp;
                    
                        洪卫
                    
                </div>

                
                    
                    <div class="info-break-policy">
                        <i class="fa fa-file-word-o fa-fw"></i>字数:&nbsp;&nbsp;
                        6.9k
                    </div>
                    

                    
                    <div class="info-break-policy">
                        <i class="fa fa-clock-o fa-fw"></i>时长:&nbsp;&nbsp;
                        35 分钟
                    </div>
                    
                
                
                
                        <span id="busuanzi_container_site_pv" style='display:none'></span>
                        <i class="fa fa-eye fa-fw"></i>浏览量:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv" ></span>
    
                

            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <blockquote>
<p>本文转载于博客园，感觉写得比较清晰，保存一下供以后查看。</p>
</blockquote>
<h1 id="GAN生成式对抗网络（一）——原理"><a href="#GAN生成式对抗网络（一）——原理" class="headerlink" title="GAN生成式对抗网络（一）——原理"></a>GAN生成式对抗网络（一）——原理</h1><p><strong>生成式对抗网络（GAN, Generative Adversarial Networks ）是一种深度学习模型</strong></p>
<h2 id="GAN包括两个核心模块"><a href="#GAN包括两个核心模块" class="headerlink" title="GAN包括两个核心模块"></a>GAN包括两个核心模块</h2><ul>
<li>1.生成器模块 –generator</li>
<li>2.判别器模块–desciminator</li>
</ul>
<h2 id="GAN通俗原理解释"><a href="#GAN通俗原理解释" class="headerlink" title="GAN通俗原理解释"></a>GAN通俗原理解释</h2><p>为了通俗的解释GAN原理，可以类比为伪造货币的例子（这个比方纯粹为了解释）<br>现在有个伪造货币的任务。</p>
<p>你有一堆真实的货币，一个可以不断提高鉴别能力的鉴定货币真伪的设备，还有一个可以提高伪造能力的伪造货币的设备。</p>
<p>1.我们继续不断的强化鉴定设备的 鉴定能力，尽全力让他能将真币识别为真币，将价比识别为价币。（鉴定结果是一个0到1之间的概率。越接近0，说明鉴定结果越是假币）</p>
<p>2.我们让伪造设备不断的伪造假币，将假币真币混合在一起，交给鉴定设备鉴定。根据鉴定结果（概率），我们不断改善伪造设备，使伪造的假币被鉴定为真的概率持续提高。<br>现在形成了矛与盾的局面。一个伪造货币设备，和鉴定货币真伪设备的持续较量，两者都不断的从对抗中吸取经验、教训，提高自己。</p>
<p>两者不断的对抗，两者的能力都持续不断的提高，最终我们得到了一个货币鉴定专家，一个伪造货币天才，而且这个伪造货币天才，学习能力超级强。将它制造的假币和真币混在一起之后，我们这个鉴定专家，已经区分不出来，都认为是真的货币 了。</p>
<p>那么，现在伪造货币设备伪造的货币，在市面上就可以认为是真的了。因为，我们那个高级的鉴别设备，都已经无法区分他是否是真的，更不要说其他普通的鉴定设备了。</p>
<h2 id="GAN原理总结"><a href="#GAN原理总结" class="headerlink" title="GAN原理总结"></a>GAN原理总结</h2><p>如上所述，GAN生成式对抗网络的原理即：在一个不断提高判断能力的判断器的持续反馈下，不断改善生成器的生成参数，直到生成器生成的结果能够通过判断器的判断。</p>
<hr>
<h1 id="GAN生成式对抗网络（二）——tensorflow代码示例"><a href="#GAN生成式对抗网络（二）——tensorflow代码示例" class="headerlink" title="GAN生成式对抗网络（二）——tensorflow代码示例"></a>GAN生成式对抗网络（二）——tensorflow代码示例</h1><h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><p><strong>当初学习时，主要学习的这个博客 <a href="https://xyang35.github.io/2017/08/22/GAN-1/" target="_blank" rel="noopener">https://xyang35.github.io/2017/08/22/GAN-1/</a> ，写的挺好的。</strong></p>
<h2 id="1-本文目的，用GAN实现最简单的例子，帮助认识GAN算法"><a href="#1-本文目的，用GAN实现最简单的例子，帮助认识GAN算法" class="headerlink" title="1. 本文目的，用GAN实现最简单的例子，帮助认识GAN算法"></a>1. 本文目的，用GAN实现最简单的例子，帮助认识GAN算法</h2><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">from</span> matplotlib <span class="token keyword">import</span> pyplot <span class="token keyword">as</span> plt
batch_size <span class="token operator">=</span> <span class="token number">4</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h2 id="2-真实数据集，我们要通过GAN学习这个数据集，然后生成和他分布规则一样的数据集"><a href="#2-真实数据集，我们要通过GAN学习这个数据集，然后生成和他分布规则一样的数据集" class="headerlink" title="2. 真实数据集，我们要通过GAN学习这个数据集，然后生成和他分布规则一样的数据集"></a>2. 真实数据集，我们要通过GAN学习这个数据集，然后生成和他分布规则一样的数据集</h2><pre class="line-numbers language-python"><code class="language-python">X <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>normal<span class="token punctuation">(</span>size<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
A <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.1</span><span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
b <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
X <span class="token operator">=</span> np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>X<span class="token punctuation">,</span> A<span class="token punctuation">)</span> <span class="token operator">+</span> b

plt<span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>X<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> X<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 等会通过这个函数，不断从中取x值，取值数量为batch_size</span>
<span class="token keyword">def</span> <span class="token function">iterate_minibatch</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> batch_size<span class="token punctuation">,</span> shuffle<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    indices <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span>x<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> shuffle<span class="token punctuation">:</span>
        np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>shuffle<span class="token punctuation">(</span>indices<span class="token punctuation">)</span>

    <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> x<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> batch_size<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">yield</span> x<span class="token punctuation">[</span>indices<span class="token punctuation">[</span>i<span class="token punctuation">:</span>i <span class="token operator">+</span> batch_size<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="https://raw.githubusercontent.com/shw2018/cdn/master/blog_files/img/How-to-Read-Paper/1.png" alt=""></p>
<h2 id="3-封装GAN对象"><a href="#3-封装GAN对象" class="headerlink" title="3.封装GAN对象"></a>3.封装GAN对象</h2><p>包含生成器，判别器</p>
<pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">GAN</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true">#初始函数，在这里对初始化模型</span>
    <span class="token keyword">def</span> <span class="token function">netG</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> z<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true">#生成器模型</span>
    <span class="token keyword">def</span> <span class="token function">netD</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true">#判别器模型</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="4-生成器netG"><a href="#4-生成器netG" class="headerlink" title="4.生成器netG"></a>4.生成器netG</h2><p>随意输入的z，通过z*w+b的矩阵运算（全连接运算），返回结果</p>
<pre class="line-numbers language-python"><code class="language-python">    <span class="token keyword">def</span> <span class="token function">netG</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> z<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token triple-quoted-string string">"""1-layer fully connected network"""</span>

        <span class="token keyword">with</span> tf<span class="token punctuation">.</span>variable_scope<span class="token punctuation">(</span><span class="token string">"generator"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> scope<span class="token punctuation">:</span>
            W <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"g_W"</span><span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>contrib<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>xavier_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                trainable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            b <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"g_b"</span><span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>zeros_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                trainable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            <span class="token keyword">return</span> tf<span class="token punctuation">.</span>matmul<span class="token punctuation">(</span>z<span class="token punctuation">,</span> W<span class="token punctuation">)</span> <span class="token operator">+</span> b<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="5-判别器nefD"><a href="#5-判别器nefD" class="headerlink" title="5.判别器nefD"></a>5.判别器nefD</h2><p>判别器为三层全连接网络。隐层部分使用tanh激活函数。输出部分没有激活函数</p>
<pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">def</span> <span class="token function">netD</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token triple-quoted-string string">"""3-layer fully connected network"""</span>

        <span class="token keyword">with</span> tf<span class="token punctuation">.</span>variable_scope<span class="token punctuation">(</span><span class="token string">"discriminator"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> scope<span class="token punctuation">:</span>
            <span class="token keyword">if</span> reuse<span class="token punctuation">:</span>
                scope<span class="token punctuation">.</span>reuse_variables<span class="token punctuation">(</span><span class="token punctuation">)</span>

            W1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"d_W1"</span><span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>contrib<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>xavier_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                 trainable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            b1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"d_b1"</span><span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>zeros_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                 trainable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            W2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"d_W2"</span><span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>contrib<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>xavier_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                 trainable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            b2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"d_b2"</span><span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>zeros_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                 trainable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            W3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"d_W3"</span><span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>contrib<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>xavier_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                 trainable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            b3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"d_b3"</span><span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>zeros_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                 trainable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

            layer1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>tanh<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>matmul<span class="token punctuation">(</span>x<span class="token punctuation">,</span> W1<span class="token punctuation">)</span> <span class="token operator">+</span> b1<span class="token punctuation">)</span>
            layer2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>tanh<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>matmul<span class="token punctuation">(</span>layer1<span class="token punctuation">,</span> W2<span class="token punctuation">)</span> <span class="token operator">+</span> b2<span class="token punctuation">)</span>
            <span class="token keyword">return</span> tf<span class="token punctuation">.</span>matmul<span class="token punctuation">(</span>layer2<span class="token punctuation">,</span> W3<span class="token punctuation">)</span> <span class="token operator">+</span> b3<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="6-初始化init函数"><a href="#6-初始化init函数" class="headerlink" title="6.初始化init函数"></a>6.初始化<strong>init</strong>函数</h2><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true"># input, output</span>
         <span class="token comment" spellcheck="true">#占位变量，等会用来保存随机产生的数，</span>
        self<span class="token punctuation">.</span>z <span class="token operator">=</span> tf<span class="token punctuation">.</span>placeholder<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>float32<span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span>None<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">'z'</span><span class="token punctuation">)</span>   
        <span class="token comment" spellcheck="true">#占位变量，真实数据的</span>
        self<span class="token punctuation">.</span>x <span class="token operator">=</span> tf<span class="token punctuation">.</span>placeholder<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>float32<span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span>None<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">'real_x'</span><span class="token punctuation">)</span>  

        <span class="token comment" spellcheck="true"># define the network</span>
        <span class="token comment" spellcheck="true">#生成器，对随机变量进行加工，产生伪造的数据</span>
        self<span class="token punctuation">.</span>fake_x <span class="token operator">=</span> self<span class="token punctuation">.</span>netG<span class="token punctuation">(</span>self<span class="token punctuation">.</span>z<span class="token punctuation">)</span>  

         <span class="token comment" spellcheck="true">#判别器对真实数据进行判别，返回判别结果</span>
         <span class="token comment" spellcheck="true">#reuse=false,表示不是共享变量，需要tensorflow开辟变量地址</span>
        self<span class="token punctuation">.</span>real_logits <span class="token operator">=</span> self<span class="token punctuation">.</span>netD<span class="token punctuation">(</span>self<span class="token punctuation">.</span>x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>  

        <span class="token comment" spellcheck="true">#判别器对伪造数据进行判别，返回判别结果</span>
         <span class="token comment" spellcheck="true">#reuse=true,表示是共享变量，复用netD中已有的变量</span>
        self<span class="token punctuation">.</span>fake_logits <span class="token operator">=</span> self<span class="token punctuation">.</span>netD<span class="token punctuation">(</span>self<span class="token punctuation">.</span>fake_x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>


        <span class="token comment" spellcheck="true"># define losses</span>
        <span class="token comment" spellcheck="true">#判定器的损失值，将真实数据的判定为真实数据，将伪造数据的判断为伪造数据的得分情况</span>
        self<span class="token punctuation">.</span>loss_D <span class="token operator">=</span> tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>real_logits<span class="token punctuation">,</span>
                                                                             labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>ones_like<span class="token punctuation">(</span>self<span class="token punctuation">.</span>real_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> \
                      tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>fake_logits<span class="token punctuation">,</span>
                                                                             labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>zeros_like<span class="token punctuation">(</span>self<span class="token punctuation">.</span>real_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true">#生成器的生成分数。伪造的数据，别判断器判定为真实数据的得分情况</span>
        self<span class="token punctuation">.</span>loss_G <span class="token operator">=</span> tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>fake_logits<span class="token punctuation">,</span>
                                                                             labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>ones_like<span class="token punctuation">(</span>self<span class="token punctuation">.</span>real_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># collect variables</span>
        t_vars <span class="token operator">=</span> tf<span class="token punctuation">.</span>trainable_variables<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true">#存放判别器中用到的变量</span>
        self<span class="token punctuation">.</span>d_vars <span class="token operator">=</span> <span class="token punctuation">[</span>var <span class="token keyword">for</span> var <span class="token keyword">in</span> t_vars <span class="token keyword">if</span> <span class="token string">'d_'</span> <span class="token keyword">in</span> var<span class="token punctuation">.</span>name<span class="token punctuation">]</span>
        <span class="token comment" spellcheck="true">#存放生成器中用到的变量</span>
        self<span class="token punctuation">.</span>g_vars <span class="token operator">=</span> <span class="token punctuation">[</span>var <span class="token keyword">for</span> var <span class="token keyword">in</span> t_vars <span class="token keyword">if</span> <span class="token string">'g_'</span> <span class="token keyword">in</span> var<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="7-开始训练"><a href="#7-开始训练" class="headerlink" title="7.开始训练"></a>7.开始训练</h2><pre class="line-numbers language-python"><code class="language-python">gan <span class="token operator">=</span> GAN<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">#使用随机梯度下降</span>
d_optim <span class="token operator">=</span> tf<span class="token punctuation">.</span>train<span class="token punctuation">.</span>AdamOptimizer<span class="token punctuation">(</span>learning_rate<span class="token operator">=</span><span class="token number">0.05</span><span class="token punctuation">)</span><span class="token punctuation">.</span>minimize<span class="token punctuation">(</span>gan<span class="token punctuation">.</span>loss_D<span class="token punctuation">,</span> var_list<span class="token operator">=</span>gan<span class="token punctuation">.</span>d_vars<span class="token punctuation">)</span>
g_optim <span class="token operator">=</span> tf<span class="token punctuation">.</span>train<span class="token punctuation">.</span>AdamOptimizer<span class="token punctuation">(</span>learning_rate<span class="token operator">=</span><span class="token number">0.01</span><span class="token punctuation">)</span><span class="token punctuation">.</span>minimize<span class="token punctuation">(</span>gan<span class="token punctuation">.</span>loss_G<span class="token punctuation">,</span> var_list<span class="token operator">=</span>gan<span class="token punctuation">.</span>g_vars<span class="token punctuation">)</span>

init <span class="token operator">=</span> tf<span class="token punctuation">.</span>global_variables_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">with</span> tf<span class="token punctuation">.</span>Session<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> sess<span class="token punctuation">:</span>
    sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span>init<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true">#将数据循环10次</span>
    <span class="token keyword">for</span> epoch <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        avg_loss <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">.</span>
        count <span class="token operator">=</span> <span class="token number">0</span>
        <span class="token comment" spellcheck="true">#从真实数据集当中，随机抓取batch_size数量个值</span>
        <span class="token keyword">for</span> x_batch <span class="token keyword">in</span> iterate_minibatch<span class="token punctuation">(</span>X<span class="token punctuation">,</span> batch_size<span class="token operator">=</span>batch_size<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token comment" spellcheck="true"># generate noise z</span>
            <span class="token comment" spellcheck="true">#随机变量，数量为batch_size</span>
            z_batch <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>normal<span class="token punctuation">(</span>size<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># update D network</span>
             <span class="token comment" spellcheck="true">#将拿到的真实数据值和随机生成的数值，喂养给sess，并bp优化一次</span>
            loss_D<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>loss_D<span class="token punctuation">,</span> d_optim<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                     gan<span class="token punctuation">.</span>z<span class="token punctuation">:</span> z_batch<span class="token punctuation">,</span>
                                     gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> x_batch<span class="token punctuation">,</span>
                                 <span class="token punctuation">}</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># update G network</span>
            loss_G<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>loss_G<span class="token punctuation">,</span> g_optim<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                     gan<span class="token punctuation">.</span>z<span class="token punctuation">:</span> z_batch<span class="token punctuation">,</span>
                                     gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span>z_batch<span class="token punctuation">.</span>shape<span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token comment" spellcheck="true"># dummy input</span>
                                 <span class="token punctuation">}</span><span class="token punctuation">)</span>

            avg_loss <span class="token operator">+=</span> loss_D
            count <span class="token operator">+=</span> <span class="token number">1</span>

        avg_loss <span class="token operator">/=</span> count
        <span class="token comment" spellcheck="true">#每一个epoch都展示一次生成效果</span>
        z <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>normal<span class="token punctuation">(</span>size<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># 随机生成100个数值，0到1000---用来从真实值里面取数据</span>
        excerpt <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">,</span> size<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">)</span>
        fake_x<span class="token punctuation">,</span> real_logits<span class="token punctuation">,</span> fake_logits <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>fake_x<span class="token punctuation">,</span> gan<span class="token punctuation">.</span>real_logits<span class="token punctuation">,</span> gan<span class="token punctuation">.</span>fake_logits<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                                    feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>gan<span class="token punctuation">.</span>z<span class="token punctuation">:</span> z<span class="token punctuation">,</span> gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> X<span class="token punctuation">[</span>excerpt<span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
        accuracy <span class="token operator">=</span> <span class="token number">0.5</span> <span class="token operator">*</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>sum<span class="token punctuation">(</span>real_logits <span class="token operator">></span> <span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">100</span><span class="token punctuation">.</span> <span class="token operator">+</span> np<span class="token punctuation">.</span>sum<span class="token punctuation">(</span>fake_logits <span class="token operator">&lt;</span> <span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">100</span><span class="token punctuation">.</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'\ndiscriminator loss at epoch %d: %f'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>epoch<span class="token punctuation">,</span> avg_loss<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'\ndiscriminator accuracy at epoch %d: %f'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>epoch<span class="token punctuation">,</span> accuracy<span class="token punctuation">)</span><span class="token punctuation">)</span>
        plt<span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>X<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> X<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        plt<span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>fake_x<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> fake_x<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="https://raw.githubusercontent.com/shw2018/cdn/master/blog_files/img/How-to-Read-Paper/2.png" alt=""><br><img src="https://raw.githubusercontent.com/shw2018/cdn/master/blog_files/img/How-to-Read-Paper/3.png" alt=""><br><img src="https://raw.githubusercontent.com/shw2018/cdn/master/blog_files/img/How-to-Read-Paper/4.png" alt=""></p>
<hr>
<h1 id="GAN生成式对抗网络（三）——mnist数据生成"><a href="#GAN生成式对抗网络（三）——mnist数据生成" class="headerlink" title="GAN生成式对抗网络（三）——mnist数据生成"></a>GAN生成式对抗网络（三）——mnist数据生成</h1><p><strong>通过GAN生成式对抗网络，产生mnist数据</strong></p>
<h2 id="引入包，数据约定等"><a href="#引入包，数据约定等" class="headerlink" title="引入包，数据约定等"></a>引入包，数据约定等</h2><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> input_data  <span class="token comment" spellcheck="true">#读取数据的一个工具文件，不影响理解</span>
<span class="token keyword">import</span> tensorflow <span class="token keyword">as</span> tf

<span class="token comment" spellcheck="true"># 获取数据</span>
mnist <span class="token operator">=</span> input_data<span class="token punctuation">.</span>read_data_sets<span class="token punctuation">(</span><span class="token string">'data/'</span><span class="token punctuation">,</span> one_hot<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
trainimg <span class="token operator">=</span> mnist<span class="token punctuation">.</span>train<span class="token punctuation">.</span>images

X <span class="token operator">=</span> mnist<span class="token punctuation">.</span>train<span class="token punctuation">.</span>images<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span>
batch_size <span class="token operator">=</span> <span class="token number">64</span>

<span class="token comment" spellcheck="true">#用来返回真实数据</span>
<span class="token keyword">def</span> <span class="token function">iterate_minibatch</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> batch_size<span class="token punctuation">,</span> shuffle<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    indices <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span>x<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> shuffle<span class="token punctuation">:</span>
        np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>shuffle<span class="token punctuation">(</span>indices<span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> x<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">-</span><span class="token number">1000</span><span class="token punctuation">,</span> batch_size<span class="token punctuation">)</span><span class="token punctuation">:</span>
        temp <span class="token operator">=</span> x<span class="token punctuation">[</span>indices<span class="token punctuation">[</span>i<span class="token punctuation">:</span>i <span class="token operator">+</span> batch_size<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span>
        temp <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>temp<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">-</span> <span class="token number">1</span>
        <span class="token keyword">yield</span> np<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>temp<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="GAN对象结构"><a href="#GAN对象结构" class="headerlink" title="GAN对象结构"></a>GAN对象结构</h2><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">GAN</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true">#初始函数，在这里对初始化模型</span>
    <span class="token keyword">def</span> <span class="token function">netG</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> z<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true">#生成器模型</span>
    <span class="token keyword">def</span> <span class="token function">netD</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true">#判别器模型</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="生成器函数"><a href="#生成器函数" class="headerlink" title="生成器函数"></a>生成器函数</h2><p>对随机值z(维度为1，100)，进行包装，伪造，产生伪造数据。</p>
<p>包装过程概括为：全连接-&gt;reshape-&gt;反卷积</p>
<p>包装过程中使用了batch_normalization，Leaky ReLU，dropout，tanh等技巧</p>
<pre class="line-numbers language-python"><code class="language-python"><span class="token comment" spellcheck="true">#对随机值z(维度为1，100)，进行包装，伪造，产生伪造数据。</span>
    <span class="token comment" spellcheck="true">#包装过程概括为：全连接->reshape->反卷积</span>
    <span class="token comment" spellcheck="true">#包装过程中使用了batch_normalization，Leaky ReLU，dropout，tanh等技巧</span>
    <span class="token keyword">def</span> <span class="token function">netG</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>z<span class="token punctuation">,</span>alpha<span class="token operator">=</span><span class="token number">0.01</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">with</span> tf<span class="token punctuation">.</span>variable_scope<span class="token punctuation">(</span><span class="token string">'generator'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> scope<span class="token punctuation">:</span>
            layer1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>dense<span class="token punctuation">(</span>z<span class="token punctuation">,</span> <span class="token number">4</span> <span class="token operator">*</span> <span class="token number">4</span> <span class="token operator">*</span> <span class="token number">512</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 这是一个全连接层，输出 (n,4*4*512)</span>
            layer1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>layer1<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">512</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># batch normalization</span>
            layer1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>layer1<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 做BN标准化处理</span>
            <span class="token comment" spellcheck="true"># Leaky ReLU</span>
            layer1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>alpha <span class="token operator">*</span> layer1<span class="token punctuation">,</span> layer1<span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># dropout</span>
            layer1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>dropout<span class="token punctuation">(</span>layer1<span class="token punctuation">,</span> keep_prob<span class="token operator">=</span><span class="token number">0.8</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 4 x 4 x 512 to 7 x 7 x 256</span>
            layer2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d_transpose<span class="token punctuation">(</span>layer1<span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'valid'</span><span class="token punctuation">)</span>
            layer2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>layer2<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            layer2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>alpha <span class="token operator">*</span> layer2<span class="token punctuation">,</span> layer2<span class="token punctuation">)</span>
            layer2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>dropout<span class="token punctuation">(</span>layer2<span class="token punctuation">,</span> keep_prob<span class="token operator">=</span><span class="token number">0.8</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 7 x 7 256 to 14 x 14 x 128</span>
            layer3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d_transpose<span class="token punctuation">(</span>layer2<span class="token punctuation">,</span> <span class="token number">128</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'same'</span><span class="token punctuation">)</span>
            layer3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>layer3<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            layer3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>alpha <span class="token operator">*</span> layer3<span class="token punctuation">,</span> layer3<span class="token punctuation">)</span>
            layer3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>dropout<span class="token punctuation">(</span>layer3<span class="token punctuation">,</span> keep_prob<span class="token operator">=</span><span class="token number">0.8</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 14 x 14 x 128 to 28 x 28 x 1</span>
            logits <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d_transpose<span class="token punctuation">(</span>layer3<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'same'</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># MNIST原始数据集的像素范围在0-1，这里的生成图片范围为(-1,1)</span>
            <span class="token comment" spellcheck="true"># 因此在训练时，记住要把MNIST像素范围进行resize</span>
            outputs <span class="token operator">=</span> tf<span class="token punctuation">.</span>tanh<span class="token punctuation">(</span>logits<span class="token punctuation">)</span>

            <span class="token keyword">return</span> outputs<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="判别器函数"><a href="#判别器函数" class="headerlink" title="判别器函数"></a>判别器函数</h2><p>通过深度卷积+全连接的形式，判别器将输入分类为真数据，还是假数据。</p>
<pre class="line-numbers language-python"><code class="language-python">    <span class="token keyword">def</span> <span class="token function">netD</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span>alpha<span class="token operator">=</span><span class="token number">0.01</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">with</span> tf<span class="token punctuation">.</span>variable_scope<span class="token punctuation">(</span><span class="token string">'discriminator'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> scope<span class="token punctuation">:</span>
            <span class="token keyword">if</span> reuse<span class="token punctuation">:</span>
                scope<span class="token punctuation">.</span>reuse_variables<span class="token punctuation">(</span><span class="token punctuation">)</span>
            layer1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token number">128</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'same'</span><span class="token punctuation">)</span>
            layer1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>alpha <span class="token operator">*</span> layer1<span class="token punctuation">,</span> layer1<span class="token punctuation">)</span>
            layer1 <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>dropout<span class="token punctuation">(</span>layer1<span class="token punctuation">,</span> keep_prob<span class="token operator">=</span><span class="token number">0.8</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 14 x 14 x 128 to 7 x 7 x 256</span>
            layer2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>layer1<span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'same'</span><span class="token punctuation">)</span>
            layer2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>layer2<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            layer2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>alpha <span class="token operator">*</span> layer2<span class="token punctuation">,</span> layer2<span class="token punctuation">)</span>
            layer2 <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>dropout<span class="token punctuation">(</span>layer2<span class="token punctuation">,</span> keep_prob<span class="token operator">=</span><span class="token number">0.8</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 7 x 7 x 256 to 4 x 4 x 512</span>
            layer3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>layer2<span class="token punctuation">,</span> <span class="token number">512</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'same'</span><span class="token punctuation">)</span>
            layer3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>layer3<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            layer3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>alpha <span class="token operator">*</span> layer3<span class="token punctuation">,</span> layer3<span class="token punctuation">)</span>
            layer3 <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>dropout<span class="token punctuation">(</span>layer3<span class="token punctuation">,</span> keep_prob<span class="token operator">=</span><span class="token number">0.8</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 4 x 4 x 512 to 4*4*512 x 1</span>
            flatten <span class="token operator">=</span> tf<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>layer3<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span> <span class="token operator">*</span> <span class="token number">4</span> <span class="token operator">*</span> <span class="token number">512</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            f <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>dense<span class="token punctuation">(</span>flatten<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
            <span class="token keyword">return</span> f<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="初始化函数"><a href="#初始化函数" class="headerlink" title="初始化函数"></a>初始化函数</h2><p>有一个前置训练，将真实数据喂给判别器，训练判别器的鉴别能力</p>
<pre class="line-numbers language-python"><code class="language-python">    <span class="token comment" spellcheck="true"># 有一个前置训练，将真实数据喂给判别器，训练判别器的鉴别能力</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        self<span class="token punctuation">.</span>z <span class="token operator">=</span> tf<span class="token punctuation">.</span>placeholder<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>float32<span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span>batch_size<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">'z'</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 随机输入值</span>
        self<span class="token punctuation">.</span>x <span class="token operator">=</span> tf<span class="token punctuation">.</span>placeholder<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>float32<span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span>batch_size<span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">'real_x'</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 图片值</span>

        self<span class="token punctuation">.</span>fake_x <span class="token operator">=</span> self<span class="token punctuation">.</span>netG<span class="token punctuation">(</span>self<span class="token punctuation">.</span>z<span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 将随机输入，包装为伪造图片值</span>

        self<span class="token punctuation">.</span>pre_logits <span class="token operator">=</span> self<span class="token punctuation">.</span>netD<span class="token punctuation">(</span>self<span class="token punctuation">.</span>x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 判别器预训练时，判别器对真实数据的判别情况-未sigmoid处理</span>
        self<span class="token punctuation">.</span>real_logits <span class="token operator">=</span> self<span class="token punctuation">.</span>netD<span class="token punctuation">(</span>self<span class="token punctuation">.</span>x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 判别器对真实数据的判别情况-未sigmoid处理</span>
        self<span class="token punctuation">.</span>fake_logits <span class="token operator">=</span> self<span class="token punctuation">.</span>netD<span class="token punctuation">(</span>self<span class="token punctuation">.</span>fake_x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 判别器对伪造数据的判别情况-未sigmoid处理</span>

        <span class="token comment" spellcheck="true"># 预训练时判别器，判别器将真实数据判定为真的得分情况。</span>
        self<span class="token punctuation">.</span>loss_pre_D <span class="token operator">=</span> tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>pre_logits<span class="token punctuation">,</span>
                                                                                 labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>ones_like<span class="token punctuation">(</span>self<span class="token punctuation">.</span>pre_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># 训练时，判别器将真实数据判定为真，将伪造数据判定为假的得分情况。</span>
        self<span class="token punctuation">.</span>loss_D <span class="token operator">=</span> tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>real_logits<span class="token punctuation">,</span>
                                                                             labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>ones_like<span class="token punctuation">(</span>self<span class="token punctuation">.</span>real_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> \
                      tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>fake_logits<span class="token punctuation">,</span>
                                                                             labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>zeros_like<span class="token punctuation">(</span>self<span class="token punctuation">.</span>fake_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># 训练时，生成器伪造的数据，被判定为真实数据的得分情况。</span>
        self<span class="token punctuation">.</span>loss_G <span class="token operator">=</span> tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>fake_logits<span class="token punctuation">,</span>
                                                                             labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>ones_like<span class="token punctuation">(</span>self<span class="token punctuation">.</span>fake_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># 获取生成器和判定器对应的变量地址，用于更新变量</span>
        t_vars <span class="token operator">=</span> tf<span class="token punctuation">.</span>trainable_variables<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>g_vars <span class="token operator">=</span> <span class="token punctuation">[</span>var <span class="token keyword">for</span> var <span class="token keyword">in</span> t_vars <span class="token keyword">if</span> var<span class="token punctuation">.</span>name<span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">"generator"</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
        self<span class="token punctuation">.</span>d_vars <span class="token operator">=</span> <span class="token punctuation">[</span>var <span class="token keyword">for</span> var <span class="token keyword">in</span> t_vars <span class="token keyword">if</span> var<span class="token punctuation">.</span>name<span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">"discriminator"</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="开始训练"><a href="#开始训练" class="headerlink" title="开始训练"></a>开始训练</h2><pre class="line-numbers language-python"><code class="language-python">gan <span class="token operator">=</span> DCGAN<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">#预训练时的梯度优化函数</span>
d_pre_optim <span class="token operator">=</span> tf<span class="token punctuation">.</span>train<span class="token punctuation">.</span>AdamOptimizer<span class="token punctuation">(</span>learning_rate<span class="token operator">=</span><span class="token number">0.001</span><span class="token punctuation">,</span> beta1<span class="token operator">=</span><span class="token number">0.4</span><span class="token punctuation">)</span><span class="token punctuation">.</span>minimize<span class="token punctuation">(</span>gan<span class="token punctuation">.</span>loss_pre_D<span class="token punctuation">,</span> var_list<span class="token operator">=</span>gan<span class="token punctuation">.</span>d_vars<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">#判别器的梯度优化函数</span>
d_optim <span class="token operator">=</span> tf<span class="token punctuation">.</span>train<span class="token punctuation">.</span>AdamOptimizer<span class="token punctuation">(</span>learning_rate<span class="token operator">=</span><span class="token number">0.001</span><span class="token punctuation">,</span> beta1<span class="token operator">=</span><span class="token number">0.4</span><span class="token punctuation">)</span><span class="token punctuation">.</span>minimize<span class="token punctuation">(</span>gan<span class="token punctuation">.</span>loss_D<span class="token punctuation">,</span> var_list<span class="token operator">=</span>gan<span class="token punctuation">.</span>d_vars<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">#预训练时的梯度优化函数</span>
g_optim <span class="token operator">=</span> tf<span class="token punctuation">.</span>train<span class="token punctuation">.</span>AdamOptimizer<span class="token punctuation">(</span>learning_rate<span class="token operator">=</span><span class="token number">0.001</span><span class="token punctuation">,</span> beta1<span class="token operator">=</span><span class="token number">0.4</span><span class="token punctuation">)</span><span class="token punctuation">.</span>minimize<span class="token punctuation">(</span>gan<span class="token punctuation">.</span>loss_G<span class="token punctuation">,</span> var_list<span class="token operator">=</span>gan<span class="token punctuation">.</span>g_vars<span class="token punctuation">)</span>

init <span class="token operator">=</span> tf<span class="token punctuation">.</span>global_variables_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">with</span> tf<span class="token punctuation">.</span>Session<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> sess<span class="token punctuation">:</span>
    sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span>init<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true">#对判别器的预训练，训练了两个epoch</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'判别器初始训练,第'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'次包'</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span> x_batch <span class="token keyword">in</span> iterate_minibatch<span class="token punctuation">(</span>X<span class="token punctuation">,</span> batch_size<span class="token operator">=</span>batch_size<span class="token punctuation">)</span><span class="token punctuation">:</span>
            loss_pre_D<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>pre_logits<span class="token punctuation">,</span> d_pre_optim<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                     feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                         gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> x_batch
                                     <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true">#训练5个epoch</span>
    <span class="token keyword">for</span> epoch <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'对抗'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'次包'</span><span class="token punctuation">)</span>
        avg_loss <span class="token operator">=</span> <span class="token number">0</span>
        count <span class="token operator">=</span> <span class="token number">0</span>
        <span class="token keyword">for</span> x_batch <span class="token keyword">in</span> iterate_minibatch<span class="token punctuation">(</span>X<span class="token punctuation">,</span> batch_size<span class="token operator">=</span>batch_size<span class="token punctuation">)</span><span class="token punctuation">:</span>
            z_batch <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>uniform<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> size<span class="token operator">=</span><span class="token punctuation">(</span>batch_size<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 随机起点值</span>

            loss_D<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>loss_D<span class="token punctuation">,</span> d_optim<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                     gan<span class="token punctuation">.</span>z<span class="token punctuation">:</span> z_batch<span class="token punctuation">,</span>
                                     gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> x_batch
                                 <span class="token punctuation">}</span><span class="token punctuation">)</span>

            loss_G<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>loss_G<span class="token punctuation">,</span> g_optim<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                     gan<span class="token punctuation">.</span>z<span class="token punctuation">:</span> z_batch<span class="token punctuation">,</span>
                                     <span class="token comment" spellcheck="true"># gan.x: np.zeros(z_batch.shape)</span>
                                 <span class="token punctuation">}</span><span class="token punctuation">)</span>

            avg_loss <span class="token operator">+=</span> loss_D
            count <span class="token operator">+=</span> <span class="token number">1</span>

        <span class="token comment" spellcheck="true"># 显示预测情况</span>
        <span class="token keyword">if</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
            avg_loss <span class="token operator">/=</span> count
            z <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>normal<span class="token punctuation">(</span>size<span class="token operator">=</span><span class="token punctuation">(</span>batch_size<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            excerpt <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> size<span class="token operator">=</span>batch_size<span class="token punctuation">)</span>
            needTest <span class="token operator">=</span> np<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>X<span class="token punctuation">[</span>excerpt<span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            fake_x<span class="token punctuation">,</span> real_logits<span class="token punctuation">,</span> fake_logits <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>fake_x<span class="token punctuation">,</span> gan<span class="token punctuation">.</span>real_logits<span class="token punctuation">,</span> gan<span class="token punctuation">.</span>fake_logits<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                                        feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>gan<span class="token punctuation">.</span>z<span class="token punctuation">:</span> z<span class="token punctuation">,</span> gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> needTest<span class="token punctuation">}</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># accuracy = (np.sum(real_logits > 0.5) + np.sum(fake_logits &lt; 0.5)) / (2 * batch_size)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'real_logits'</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>len<span class="token punctuation">(</span>real_logits<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'fake_logits'</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>len<span class="token punctuation">(</span>fake_logits<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'\ndiscriminator loss at epoch %d: %f'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>epoch<span class="token punctuation">,</span> avg_loss<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># print('\ndiscriminator accuracy at epoch %d: %f' % (epoch, accuracy))</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'----'</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># curr_img = np.reshape(trainimg[i, :], (28, 28))  # 28 by 28 matrix</span>
            curr_img <span class="token operator">=</span> np<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>fake_x<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>matshow<span class="token punctuation">(</span>curr_img<span class="token punctuation">,</span> cmap<span class="token operator">=</span>plt<span class="token punctuation">.</span>get_cmap<span class="token punctuation">(</span><span class="token string">'gray'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
            curr_img2 <span class="token operator">=</span> np<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>fake_x<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>matshow<span class="token punctuation">(</span>curr_img2<span class="token punctuation">,</span> cmap<span class="token operator">=</span>plt<span class="token punctuation">.</span>get_cmap<span class="token punctuation">(</span><span class="token string">'gray'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
            curr_img3 <span class="token operator">=</span> np<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>fake_x<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>matshow<span class="token punctuation">(</span>curr_img3<span class="token punctuation">,</span> cmap<span class="token operator">=</span>plt<span class="token punctuation">.</span>get_cmap<span class="token punctuation">(</span><span class="token string">'gray'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>

            curr_img4 <span class="token operator">=</span> np<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>fake_x<span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>matshow<span class="token punctuation">(</span>curr_img4<span class="token punctuation">,</span> cmap<span class="token operator">=</span>plt<span class="token punctuation">.</span>get_cmap<span class="token punctuation">(</span><span class="token string">'gray'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>

            curr_img5 <span class="token operator">=</span> np<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>fake_x<span class="token punctuation">[</span><span class="token number">40</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>matshow<span class="token punctuation">(</span>curr_img5<span class="token punctuation">,</span> cmap<span class="token operator">=</span>plt<span class="token punctuation">.</span>get_cmap<span class="token punctuation">(</span><span class="token string">'gray'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># plt.figure(figsize=(28, 28))</span>

            <span class="token comment" spellcheck="true"># plt.title("" + str(i) + "th Training Data "</span>
            <span class="token comment" spellcheck="true">#           + "Label is " + str(curr_label))</span>
            <span class="token comment" spellcheck="true"># print("" + str(i) + "th Training Data "</span>
            <span class="token comment" spellcheck="true">#       + "Label is " + str(curr_label))</span>

            <span class="token comment" spellcheck="true"># plt.scatter(X[:, 0], X[:, 1])</span>
            <span class="token comment" spellcheck="true"># plt.scatter(fake_x[:, 0], fake_x[:, 1])</span>
            <span class="token comment" spellcheck="true"># plt.show()</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h2><p><img src="https://raw.githubusercontent.com/shw2018/cdn/master/blog_files/img/How-to-Read-Paper/5.jpg" alt=""></p>
<h1 id="GAN生成式对抗网络（四）——SRGAN超高分辨率图片重构"><a href="#GAN生成式对抗网络（四）——SRGAN超高分辨率图片重构" class="headerlink" title="GAN生成式对抗网络（四）——SRGAN超高分辨率图片重构"></a>GAN生成式对抗网络（四）——SRGAN超高分辨率图片重构</h1><p>论文pdf 地址：<a href="https://arxiv.org/pdf/1609.04802v1.pdf" target="_blank" rel="noopener">https://arxiv.org/pdf/1609.04802v1.pdf</a></p>
<h2 id="实际效果"><a href="#实际效果" class="headerlink" title="实际效果"></a>实际效果</h2><ul>
<li>清晰度距离我的期待有距离。</li>
<li>颜色上面存在差距。</li>
<li>解决想法</li>
<li>增加一个颜色判别器。将颜色值反馈给生成器</li>
</ul>
<p><img src="https://raw.githubusercontent.com/shw2018/cdn/master/blog_files/img/How-to-Read-Paper/6.png" alt=""></p>
<p>srgan论文是建立在gan基础上的，利用gan生成式对抗网络，将图片重构为高清分辨率的图片。</p>
<p>github上有开源的srgan项目。由于开源者，开发时考虑的问题更丰富，技巧更为高明，导致其代码都比较难以阅读和理解。</p>
<p>在为了充分理解这个论文。这里结合论文，开源代码，和自己的理解重新写了个srgan高清分辨率模型。</p>
<h2 id="GAN原理"><a href="#GAN原理" class="headerlink" title="GAN原理"></a>GAN原理</h2><p>在一个不断提高判断能力的判断器的持续反馈下，不断改善生成器的生成参数，直到生成器生成的结果能够通过判断器的判断。（见本博客其他文章）</p>
<h2 id="SRGAN用到的模块，及其关系"><a href="#SRGAN用到的模块，及其关系" class="headerlink" title="SRGAN用到的模块，及其关系"></a>SRGAN用到的模块，及其关系</h2><p><img src="https://raw.githubusercontent.com/shw2018/cdn/master/blog_files/img/How-to-Read-Paper/7.png" alt=""><br>损失值，根据的这个关系结构计算的。</p>
<blockquote>
<p>注意：vgg19是使用已经训练好的模型，这里只是拿来提取特征使用，</p>
</blockquote>
<p>对于生成器，根据三个运算结果数据，进行随机梯度的优化调整</p>
<ul>
<li>①判定器生成数据的鉴定结果</li>
<li>②vgg19的特征比较情况</li>
<li>③生成图形与理想图形的mse差距</li>
</ul>
<h2 id="论文中，生成器和判别器的模型图"><a href="#论文中，生成器和判别器的模型图" class="headerlink" title="论文中，生成器和判别器的模型图"></a>论文中，生成器和判别器的模型图</h2><p><img src="https://raw.githubusercontent.com/shw2018/cdn/master/blog_files/img/How-to-Read-Paper/8.png" alt=""><br>生成器结构为：一层卷积，16层残差卷积，再将第一层卷积结果+16层残差结，卷积+2倍反卷积，卷积+2倍反卷积，tanh缩放，产生生成结果。</p>
<p>判别器结构为：8层卷积+reshape,全连接。（论文中，用了两层。我这里只用了一层全连接，参数量太大，我6G 的gpu内存不够用）</p>
<p>vgg19结构：在vgg19的第四层，返回获取到的特征结果，进行MSE对比</p>
<blockquote>
<p>注意：BN处理，leaky relu等等处理技巧</p>
</blockquote>
<h2 id="代码解释"><a href="#代码解释" class="headerlink" title="代码解释"></a>代码解释</h2><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> os
<span class="token keyword">import</span> tensorlayer <span class="token keyword">as</span> tl
<span class="token keyword">import</span> tensorflow <span class="token keyword">as</span> tf

<span class="token comment" spellcheck="true">#获取vgg9.npy中vgg19的参数， </span>
vgg19_npy_path <span class="token operator">=</span> <span class="token string">"./vgg19.npy"</span>
<span class="token keyword">if</span> <span class="token operator">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>isfile<span class="token punctuation">(</span>vgg19_npy_path<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Please download vgg19.npz from : https://github.com/machrisaa/tensorflow-vgg"</span><span class="token punctuation">)</span>
    exit<span class="token punctuation">(</span><span class="token punctuation">)</span>
npz <span class="token operator">=</span> np<span class="token punctuation">.</span>load<span class="token punctuation">(</span>vgg19_npy_path<span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'latin1'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>item<span class="token punctuation">(</span><span class="token punctuation">)</span>
w_params <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
b_params <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> val <span class="token keyword">in</span> sorted<span class="token punctuation">(</span>npz<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    W <span class="token operator">=</span> np<span class="token punctuation">.</span>asarray<span class="token punctuation">(</span>val<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    b <span class="token operator">=</span> np<span class="token punctuation">.</span>asarray<span class="token punctuation">(</span>val<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># print("  Loading %s: %s, %s" % (val[0], W.shape, b.shape))</span>
    w_params<span class="token punctuation">.</span>append<span class="token punctuation">(</span>W<span class="token punctuation">,</span> <span class="token punctuation">)</span>
    b_params<span class="token punctuation">.</span>extend<span class="token punctuation">(</span>b<span class="token punctuation">)</span>


<span class="token comment" spellcheck="true">#tensorlayer加载图片时，用于处理图片。随机获取图片中 192*192的矩阵， 内存不足时，可以优化这里</span>
<span class="token keyword">def</span> <span class="token function">crop_sub_imgs_fn</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> is_random<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    x <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>crop<span class="token punctuation">(</span>x<span class="token punctuation">,</span> wrg<span class="token operator">=</span><span class="token number">192</span><span class="token punctuation">,</span> hrg<span class="token operator">=</span><span class="token number">192</span><span class="token punctuation">,</span> is_random<span class="token operator">=</span>is_random<span class="token punctuation">)</span>
    x <span class="token operator">=</span> x <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">.</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">)</span>
    x <span class="token operator">=</span> x <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">.</span>
    <span class="token keyword">return</span> x
<span class="token comment" spellcheck="true">#resize矩阵 内存不足时，可以优化这里</span>
<span class="token keyword">def</span> <span class="token function">downsample_fn</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">:</span>
    x <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>imresize<span class="token punctuation">(</span>x<span class="token punctuation">,</span> size<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">48</span><span class="token punctuation">,</span> <span class="token number">48</span><span class="token punctuation">]</span><span class="token punctuation">,</span> interp<span class="token operator">=</span><span class="token string">'bicubic'</span><span class="token punctuation">,</span> mode<span class="token operator">=</span>None<span class="token punctuation">)</span>
    x <span class="token operator">=</span> x <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">.</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">)</span>
    x <span class="token operator">=</span> x <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">.</span>
    <span class="token keyword">return</span> x

<span class="token comment" spellcheck="true"># 参数</span>
config <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token string">"epoch"</span><span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true"># 内存不够时，可以减小这个</span>
batch_size <span class="token operator">=</span> <span class="token number">10</span> 


<span class="token keyword">class</span> <span class="token class-name">SRGAN</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true"># with tf.device('/gpu:0'):</span>
        <span class="token comment" spellcheck="true">#占位变量，存储需要重构的图片</span>
        self<span class="token punctuation">.</span>x <span class="token operator">=</span> tf<span class="token punctuation">.</span>placeholder<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>float32<span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span>batch_size<span class="token punctuation">,</span> <span class="token number">48</span><span class="token punctuation">,</span> <span class="token number">48</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">'train_bechanged'</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true">#占位变量，存储需要学习的理想中的图片</span>
        self<span class="token punctuation">.</span>y <span class="token operator">=</span> tf<span class="token punctuation">.</span>placeholder<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>float32<span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span>batch_size<span class="token punctuation">,</span> <span class="token number">192</span><span class="token punctuation">,</span> <span class="token number">192</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">'train_target'</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>init_fake_y <span class="token operator">=</span> self<span class="token punctuation">.</span>generator<span class="token punctuation">(</span>self<span class="token punctuation">.</span>x<span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 预训练时生成的假照片</span>
        self<span class="token punctuation">.</span>fake_y <span class="token operator">=</span> self<span class="token punctuation">.</span>generator<span class="token punctuation">(</span>self<span class="token punctuation">.</span>x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 全部训练时生成的假照片</span>

         <span class="token comment" spellcheck="true">#占位变量，存储需要重构的测试图片</span>
        self<span class="token punctuation">.</span>test_x <span class="token operator">=</span> tf<span class="token punctuation">.</span>placeholder<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>float32<span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> None<span class="token punctuation">,</span> None<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">'test_generator'</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true">#占位变量，存储重构后的测试图片</span>
        self<span class="token punctuation">.</span>test_fake_y <span class="token operator">=</span> self<span class="token punctuation">.</span>generator<span class="token punctuation">(</span>self<span class="token punctuation">.</span>test_x<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 生成的假照片</span>

        <span class="token comment" spellcheck="true">#占位变量，将生成图片resize</span>
        self<span class="token punctuation">.</span>fake_y_vgg <span class="token operator">=</span> tf<span class="token punctuation">.</span>image<span class="token punctuation">.</span>resize_images<span class="token punctuation">(</span>
            self<span class="token punctuation">.</span>fake_y<span class="token punctuation">,</span> size<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">224</span><span class="token punctuation">]</span><span class="token punctuation">,</span> method<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>
            align_corners<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
         <span class="token comment" spellcheck="true">#占位变量，将理想图片resize</span>
        self<span class="token punctuation">.</span>real_y_vgg <span class="token operator">=</span> tf<span class="token punctuation">.</span>image<span class="token punctuation">.</span>resize_images<span class="token punctuation">(</span>
            self<span class="token punctuation">.</span>y<span class="token punctuation">,</span> size<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">224</span><span class="token punctuation">]</span><span class="token punctuation">,</span> method<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>
            align_corners<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true">#提取伪造图片的特征</span>
        self<span class="token punctuation">.</span>fake_y_feature <span class="token operator">=</span> self<span class="token punctuation">.</span>vgg19<span class="token punctuation">(</span>self<span class="token punctuation">.</span>fake_y_vgg<span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 假照片的特征值</span>
        <span class="token comment" spellcheck="true">#提取理想图片的特征</span>
        self<span class="token punctuation">.</span>real_y_feature <span class="token operator">=</span> self<span class="token punctuation">.</span>vgg19<span class="token punctuation">(</span>self<span class="token punctuation">.</span>real_y_vgg<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 真照片的特征值</span>

        <span class="token comment" spellcheck="true"># self.pre_dis_logits = self.discriminator(self.fake_y)  # 判别器生成的预测照片的判别值</span>
        self<span class="token punctuation">.</span>fake_dis_logits <span class="token operator">=</span> self<span class="token punctuation">.</span>discriminator<span class="token punctuation">(</span>self<span class="token punctuation">.</span>fake_y<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 判别器生成的假照片的判别值</span>
        self<span class="token punctuation">.</span>real_dis_logits <span class="token operator">=</span> self<span class="token punctuation">.</span>discriminator<span class="token punctuation">(</span>self<span class="token punctuation">.</span>y<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 判别器生成的假照片的判别值</span>

        <span class="token comment" spellcheck="true"># 预训练时，判别器的优化根据值</span>
        self<span class="token punctuation">.</span>init_mse_loss <span class="token operator">=</span> tf<span class="token punctuation">.</span>losses<span class="token punctuation">.</span>mean_squared_error<span class="token punctuation">(</span>self<span class="token punctuation">.</span>init_fake_y<span class="token punctuation">,</span> self<span class="token punctuation">.</span>y<span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># 关于判别器的优化根据值</span>
        self<span class="token punctuation">.</span>D_loos <span class="token operator">=</span> tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>real_dis_logits<span class="token punctuation">,</span>
                                                                             labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>ones_like<span class="token punctuation">(</span>
                                                                                 self<span class="token punctuation">.</span>real_dis_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> \
                      tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>fake_dis_logits<span class="token punctuation">,</span>
                                                                             labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>zeros_like<span class="token punctuation">(</span>
                                                                                 self<span class="token punctuation">.</span>fake_dis_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># 伪造数据判别器的判断情况，生成与目标图像的差距，生成特征与理想特征的差距</span>
        self<span class="token punctuation">.</span>D_loos_Ge <span class="token operator">=</span> tf<span class="token punctuation">.</span>reduce_mean<span class="token punctuation">(</span>tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>sigmoid_cross_entropy_with_logits<span class="token punctuation">(</span>logits<span class="token operator">=</span>self<span class="token punctuation">.</span>fake_dis_logits<span class="token punctuation">,</span> labels<span class="token operator">=</span>tf<span class="token punctuation">.</span>ones_like<span class="token punctuation">(</span> self<span class="token punctuation">.</span>fake_dis_logits<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>mse_loss <span class="token operator">=</span> tf<span class="token punctuation">.</span>losses<span class="token punctuation">.</span>mean_squared_error<span class="token punctuation">(</span>self<span class="token punctuation">.</span>fake_y<span class="token punctuation">,</span> self<span class="token punctuation">.</span>y<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>loss_vgg <span class="token operator">=</span> tf<span class="token punctuation">.</span>losses<span class="token punctuation">.</span>mean_squared_error<span class="token punctuation">(</span>self<span class="token punctuation">.</span>fake_y_feature<span class="token punctuation">,</span> self<span class="token punctuation">.</span>real_y_feature<span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true">#生成器的优化根据值，上面三个值的和</span>
        self<span class="token punctuation">.</span>G_loos <span class="token operator">=</span> <span class="token number">1e</span><span class="token operator">-</span><span class="token number">3</span> <span class="token operator">*</span> self<span class="token punctuation">.</span>D_loos_Ge <span class="token operator">+</span> <span class="token number">2e</span><span class="token operator">-</span><span class="token number">6</span> <span class="token operator">*</span> self<span class="token punctuation">.</span>loss_vgg <span class="token operator">+</span> self<span class="token punctuation">.</span>mse_loss

        <span class="token comment" spellcheck="true">#获取具体条件下的更新变量集合。</span>
        t_vars <span class="token operator">=</span> tf<span class="token punctuation">.</span>trainable_variables<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>g_vars <span class="token operator">=</span> <span class="token punctuation">[</span>var <span class="token keyword">for</span> var <span class="token keyword">in</span> t_vars <span class="token keyword">if</span> var<span class="token punctuation">.</span>name<span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">'trainGenerator'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
        self<span class="token punctuation">.</span>d_vars <span class="token operator">=</span> <span class="token punctuation">[</span>var <span class="token keyword">for</span> var <span class="token keyword">in</span> t_vars <span class="token keyword">if</span> var<span class="token punctuation">.</span>name<span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">'discriminator'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>



    <span class="token comment" spellcheck="true"># 生成器，16层深度残差+1层初始的深度残差+2次2倍反卷积+1个卷积</span>
    <span class="token keyword">def</span> <span class="token function">generator</span><span class="token punctuation">(</span>slef<span class="token punctuation">,</span> input<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">with</span> tf<span class="token punctuation">.</span>variable_scope<span class="token punctuation">(</span><span class="token string">'trainGenerator'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> scope<span class="token punctuation">:</span>
            <span class="token keyword">if</span> reuse<span class="token punctuation">:</span>
                scope<span class="token punctuation">.</span>reuse_variables<span class="token punctuation">(</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>input<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            prellu_param <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span><span class="token string">'p_alpha'</span><span class="token punctuation">,</span> n<span class="token punctuation">.</span>get_shape<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>constant_initializer<span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                           dtype<span class="token operator">=</span>tf<span class="token punctuation">.</span>float32<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token operator">+</span> prellu_param <span class="token operator">*</span> <span class="token punctuation">(</span>n <span class="token operator">-</span> abs<span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">0.02</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.relu(n)</span>
            temp <span class="token operator">=</span> n
            <span class="token comment" spellcheck="true"># 开始深度残差网络</span>
            <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                nn <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                      bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
                nn <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>nn<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
                prellu_param <span class="token operator">=</span> tf<span class="token punctuation">.</span>get_variable<span class="token punctuation">(</span><span class="token string">'p_alpha'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span><span class="token number">2</span> <span class="token operator">*</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> n<span class="token punctuation">.</span>get_shape<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                               initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>constant_initializer<span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                               dtype<span class="token operator">=</span>tf<span class="token punctuation">.</span>float32<span class="token punctuation">)</span>
                nn <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>nn<span class="token punctuation">)</span> <span class="token operator">+</span> prellu_param <span class="token operator">*</span> <span class="token punctuation">(</span>nn <span class="token operator">-</span> abs<span class="token punctuation">(</span>nn<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">0.02</span>

                nn <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>nn<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                      bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
                nn <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>nn<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
                <span class="token comment" spellcheck="true"># prellu_param = tf.get_variable('p_alpha' + str(2 * i + 2), n.get_shape()[-1],</span>
                <span class="token comment" spellcheck="true">#                                initializer=tf.constant_initializer(0.0),</span>
                <span class="token comment" spellcheck="true">#                                dtype=tf.float32)</span>
                <span class="token comment" spellcheck="true"># nn = tf.nn.relu(nn) + prellu_param * (nn - abs(nn)) * 0.02</span>
                n <span class="token operator">=</span> nn <span class="token operator">+</span> n

            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># prellu_param = tf.get_variable('p_alpha_34', n.get_shape()[-1],</span>
            <span class="token comment" spellcheck="true">#                                initializer=tf.constant_initializer(0.0),</span>
            <span class="token comment" spellcheck="true">#                                dtype=tf.float32)</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.relu(n) + prellu_param * (n - abs(n)) * 0.02</span>

            <span class="token comment" spellcheck="true">#注意这里的temp,看论文里面的生成器结构图</span>
            n <span class="token operator">=</span> temp <span class="token operator">+</span> n

            <span class="token comment" spellcheck="true"># 将特征还原为图</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d_transpose<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                           bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>

            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>

            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d_transpose<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                           bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>

            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>tanh<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            <span class="token keyword">return</span> n


    <span class="token comment" spellcheck="true">#判别器</span>
    <span class="token keyword">def</span> <span class="token function">discriminator</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> input<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true"># input   size： 384x384</span>
        <span class="token keyword">with</span> tf<span class="token punctuation">.</span>variable_scope<span class="token punctuation">(</span><span class="token string">'discriminator'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> scope<span class="token punctuation">:</span>
            <span class="token keyword">if</span> reuse<span class="token punctuation">:</span>
                scope<span class="token punctuation">.</span>reuse_variables<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># 1</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>input<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.01</span> <span class="token operator">*</span> n<span class="token punctuation">,</span> n<span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># 2</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.01</span> <span class="token operator">*</span> n<span class="token punctuation">,</span> n<span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 3</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">128</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.01</span> <span class="token operator">*</span> n<span class="token punctuation">,</span> n<span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 4</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">128</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.01</span> <span class="token operator">*</span> n<span class="token punctuation">,</span> n<span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 5</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.01</span> <span class="token operator">*</span> n<span class="token punctuation">,</span> n<span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 6</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.01</span> <span class="token operator">*</span> n<span class="token punctuation">,</span> n<span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 7</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">512</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.01</span> <span class="token operator">*</span> n<span class="token punctuation">,</span> n<span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># 8</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token number">512</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">,</span> activation<span class="token operator">=</span>None<span class="token punctuation">,</span> use_bias<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>
                                 bias_initializer<span class="token operator">=</span>None<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>batch_normalization<span class="token punctuation">(</span>n<span class="token punctuation">,</span> training<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.01</span> <span class="token operator">*</span> n<span class="token punctuation">,</span> n<span class="token punctuation">)</span>

            flatten <span class="token operator">=</span> tf<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token punctuation">(</span>input<span class="token punctuation">.</span>get_shape<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># 内存不够，减小全链接数量</span>
            <span class="token comment" spellcheck="true"># f = tf.layers.dense(flatten, 1024)</span>
            <span class="token comment" spellcheck="true"># 论文里面这里时leaky relu，这我用的dense里面自带的</span>
            f <span class="token operator">=</span> tf<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>dense<span class="token punctuation">(</span>flatten<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> bias_initializer<span class="token operator">=</span>tf<span class="token punctuation">.</span>contrib<span class="token punctuation">.</span>layers<span class="token punctuation">.</span>xavier_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

            <span class="token keyword">return</span> f
    <span class="token comment" spellcheck="true">#vgg19特征提取</span>
    <span class="token keyword">def</span> <span class="token function">vgg19</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> input<span class="token punctuation">,</span> reuse<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        VGG_MEAN <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">103.939</span><span class="token punctuation">,</span> <span class="token number">116.779</span><span class="token punctuation">,</span> <span class="token number">123.68</span><span class="token punctuation">]</span>
        <span class="token keyword">with</span> tf<span class="token punctuation">.</span>variable_scope<span class="token punctuation">(</span><span class="token string">'vgg19'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> scope<span class="token punctuation">:</span>
            <span class="token comment" spellcheck="true"># if reuse:</span>
            <span class="token comment" spellcheck="true">#     scope.reuse_variables()</span>
            <span class="token comment" spellcheck="true"># ====================</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"build model started"</span><span class="token punctuation">)</span>
            rgb_scaled <span class="token operator">=</span> <span class="token punctuation">(</span>input <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token number">255.0</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># Convert RGB to BGR</span>
            red<span class="token punctuation">,</span> green<span class="token punctuation">,</span> blue <span class="token operator">=</span> tf<span class="token punctuation">.</span>split<span class="token punctuation">(</span>rgb_scaled<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
            <span class="token keyword">assert</span> red<span class="token punctuation">.</span>get_shape<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>as_list<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token punctuation">[</span><span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>
            <span class="token keyword">assert</span> green<span class="token punctuation">.</span>get_shape<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>as_list<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token punctuation">[</span><span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>
            <span class="token keyword">assert</span> blue<span class="token punctuation">.</span>get_shape<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>as_list<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token punctuation">[</span><span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>
            bgr <span class="token operator">=</span> tf<span class="token punctuation">.</span>concat<span class="token punctuation">(</span>
                <span class="token punctuation">[</span>
                    blue <span class="token operator">-</span> VGG_MEAN<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                    green <span class="token operator">-</span> VGG_MEAN<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                    red <span class="token operator">-</span> VGG_MEAN<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                <span class="token punctuation">]</span><span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">)</span>
            <span class="token keyword">assert</span> bgr<span class="token punctuation">.</span>get_shape<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>as_list<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token punctuation">[</span><span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">224</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

            <span class="token comment" spellcheck="true"># --------------------</span>

            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>bgr<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">'conv2_1'</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">'conv2_2'</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>max_pool<span class="token punctuation">(</span>n<span class="token punctuation">,</span> ksize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>

            <span class="token comment" spellcheck="true"># return n</span>

            <span class="token comment" spellcheck="true"># two</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>max_pool<span class="token punctuation">(</span>n<span class="token punctuation">,</span> ksize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># three</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>max_pool<span class="token punctuation">(</span>n<span class="token punctuation">,</span> ksize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># four</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>

            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>conv2d<span class="token punctuation">(</span>n<span class="token punctuation">,</span> w_params<span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>add<span class="token punctuation">(</span>n<span class="token punctuation">,</span> b_params<span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            n <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>max_pool<span class="token punctuation">(</span>n<span class="token punctuation">,</span> ksize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token string">'SAME'</span><span class="token punctuation">)</span>
            <span class="token keyword">return</span> n

            <span class="token comment" spellcheck="true"># # five</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.conv2d(n, w_params[12], strides=(1, 1, 1, 1), padding='SAME')</span>
            <span class="token comment" spellcheck="true"># n = tf.add(n, b_params[12])</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.relu(n)</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.conv2d(n, w_params[13], strides=(1, 1, 1, 1), padding='SAME')</span>
            <span class="token comment" spellcheck="true"># n = tf.add(n, b_params[13])</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.relu(n)</span>
            <span class="token comment" spellcheck="true">#</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.conv2d(n, w_params[14], strides=(1, 1, 1, 1), padding='SAME')</span>
            <span class="token comment" spellcheck="true"># n = tf.add(n, b_params[14])</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.relu(n)</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.conv2d(n, w_params[15], strides=(1, 1, 1, 1), padding='SAME')</span>
            <span class="token comment" spellcheck="true"># n = tf.add(n, b_params[15])</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.relu(n)</span>
            <span class="token comment" spellcheck="true"># n = tf.nn.max_pool(n, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding='SAME')</span>
            <span class="token comment" spellcheck="true"># return n</span>

            <span class="token comment" spellcheck="true"># 这里拿特征进行mse对比，不需要后面的全连接</span>
            <span class="token comment" spellcheck="true"># flatten = tf.reshape(n, (input.get_shape()[0], -1))</span>
            <span class="token comment" spellcheck="true"># f = tf.layers.dense(flatten, 4096)</span>
            <span class="token comment" spellcheck="true"># f = tf.layers.dense(f, 4096)</span>
            <span class="token comment" spellcheck="true"># f = tf.layers.dense(f, 1)</span>
            <span class="token comment" spellcheck="true"># return n</span>


gan <span class="token operator">=</span> SRGAN<span class="token punctuation">(</span><span class="token punctuation">)</span>
G_OPTIM_init <span class="token operator">=</span> tf<span class="token punctuation">.</span>train<span class="token punctuation">.</span>AdamOptimizer<span class="token punctuation">(</span>learning_rate<span class="token operator">=</span><span class="token number">0.001</span><span class="token punctuation">,</span> beta1<span class="token operator">=</span><span class="token number">0.4</span><span class="token punctuation">)</span><span class="token punctuation">.</span>minimize<span class="token punctuation">(</span>gan<span class="token punctuation">.</span>init_mse_loss<span class="token punctuation">,</span> var_list<span class="token operator">=</span>gan<span class="token punctuation">.</span>g_vars<span class="token punctuation">)</span>
D_OPTIM <span class="token operator">=</span> tf<span class="token punctuation">.</span>train<span class="token punctuation">.</span>AdamOptimizer<span class="token punctuation">(</span>learning_rate<span class="token operator">=</span><span class="token number">0.001</span><span class="token punctuation">,</span> beta1<span class="token operator">=</span><span class="token number">0.4</span><span class="token punctuation">)</span><span class="token punctuation">.</span>minimize<span class="token punctuation">(</span>gan<span class="token punctuation">.</span>D_loos<span class="token punctuation">,</span> var_list<span class="token operator">=</span>gan<span class="token punctuation">.</span>d_vars<span class="token punctuation">)</span>
G_OPTIM <span class="token operator">=</span> tf<span class="token punctuation">.</span>train<span class="token punctuation">.</span>AdamOptimizer<span class="token punctuation">(</span>learning_rate<span class="token operator">=</span><span class="token number">0.001</span><span class="token punctuation">,</span> beta1<span class="token operator">=</span><span class="token number">0.4</span><span class="token punctuation">)</span><span class="token punctuation">.</span>minimize<span class="token punctuation">(</span>gan<span class="token punctuation">.</span>G_loos<span class="token punctuation">,</span> var_list<span class="token operator">=</span>gan<span class="token punctuation">.</span>g_vars<span class="token punctuation">)</span>

saver <span class="token operator">=</span> tf<span class="token punctuation">.</span>train<span class="token punctuation">.</span>Saver<span class="token punctuation">(</span>max_to_keep<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">)</span>

init <span class="token operator">=</span> tf<span class="token punctuation">.</span>global_variables_initializer<span class="token punctuation">(</span><span class="token punctuation">)</span>


<span class="token comment" spellcheck="true">#加载路径文件夹中的训练图片，这里加载的只是图片目录。防止内存中加载太多图片，内存不够   </span>
train_hr_img_list <span class="token operator">=</span> sorted<span class="token punctuation">(</span>tl<span class="token punctuation">.</span>files<span class="token punctuation">.</span>load_file_list<span class="token punctuation">(</span>path<span class="token operator">=</span><span class="token string">'F:\\theRoleOfCOde\深度学习\SRGAN_PF\gaoqing'</span><span class="token punctuation">,</span> regx<span class="token operator">=</span><span class="token string">'.*.png'</span><span class="token punctuation">,</span> printable<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">100</span><span class="token punctuation">]</span>
<span class="token comment" spellcheck="true">#加载图片  </span>
train_hr_imgs <span class="token operator">=</span> tl<span class="token punctuation">.</span>vis<span class="token punctuation">.</span>read_images<span class="token punctuation">(</span>train_hr_img_list<span class="token punctuation">,</span> path<span class="token operator">=</span><span class="token string">'F:\\theRoleOfCOde\深度学习\SRGAN_PF\gaoqing'</span><span class="token punctuation">,</span> n_threads<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">#加载路径文件夹中的测试图片目录</span>
test_img_list <span class="token operator">=</span> sorted<span class="token punctuation">(</span> tl<span class="token punctuation">.</span>files<span class="token punctuation">.</span>load_file_list<span class="token punctuation">(</span>path<span class="token operator">=</span><span class="token string">'F:\\theRoleOfCOde\深度学习\SRGAN_PF\SRGAN_PF\img\\test'</span><span class="token punctuation">,</span> regx<span class="token operator">=</span><span class="token string">'.*.png'</span><span class="token punctuation">,</span> printable<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span> <span class="token punctuation">:</span><span class="token number">6</span><span class="token punctuation">]</span>
test_img <span class="token operator">=</span> tl<span class="token punctuation">.</span>vis<span class="token punctuation">.</span>read_images<span class="token punctuation">(</span>test_img_list<span class="token punctuation">,</span> path<span class="token operator">=</span><span class="token string">'F:\\theRoleOfCOde\深度学习\SRGAN_PF\SRGAN_PF\img\\test'</span><span class="token punctuation">,</span> n_threads<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>



<span class="token comment" spellcheck="true">#分三种运行方式，</span>
<span class="token comment" spellcheck="true">#pre，预训练判别器</span>
<span class="token comment" spellcheck="true">#restore,回复训练好的模型，继续训练</span>


<span class="token comment" spellcheck="true">#训练一会儿，就测试一下效果。将生成的图片矩阵，保存为numpy矩阵</span>
<span class="token comment" spellcheck="true">#通过工具函数，变化为图片查看</span>
<span class="token comment" spellcheck="true">#第三种，从零开始训练</span>
<span class="token keyword">with</span> tf<span class="token punctuation">.</span>Session<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> sess<span class="token punctuation">:</span>
    type <span class="token operator">=</span> <span class="token string">'go'</span>
    <span class="token keyword">if</span> type <span class="token operator">==</span> <span class="token string">'restore'</span><span class="token punctuation">:</span>
        saver<span class="token punctuation">.</span>restore<span class="token punctuation">(</span>sess<span class="token punctuation">,</span> <span class="token string">"./save/nets/ckpt-0-80"</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'---------------------恢复以前的训练数据，继续训练-----------------------'</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span> epoch <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">for</span> idx <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>len<span class="token punctuation">(</span>train_hr_imgs<span class="token punctuation">)</span> <span class="token operator">//</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span> batch_size<span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token comment" spellcheck="true"># print(type(train_hr_imgs[idx:idx + batch_size]))</span>
                b_imgs_384 <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>threading_data<span class="token punctuation">(</span>train_hr_imgs<span class="token punctuation">[</span>idx<span class="token punctuation">:</span>idx <span class="token operator">+</span> batch_size<span class="token punctuation">]</span><span class="token punctuation">,</span> fn<span class="token operator">=</span>crop_sub_imgs_fn<span class="token punctuation">,</span>
                                                      is_random<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
                b_imgs_96 <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>threading_data<span class="token punctuation">(</span>b_imgs_384<span class="token punctuation">,</span> fn<span class="token operator">=</span>downsample_fn<span class="token punctuation">)</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'-------------pre_generator:'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'----------------'</span><span class="token punctuation">)</span>
                <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                    init_mse_loss<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>init_mse_loss<span class="token punctuation">,</span> G_OPTIM_init<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                                feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                                    gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> b_imgs_96<span class="token punctuation">,</span>
                                                    gan<span class="token punctuation">.</span>y<span class="token punctuation">:</span> b_imgs_384
                                                <span class="token punctuation">}</span><span class="token punctuation">)</span>
                    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'init_mse_loss:'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>init_mse_loss<span class="token punctuation">)</span><span class="token punctuation">)</span>
            saver<span class="token punctuation">.</span>save<span class="token punctuation">(</span>sess<span class="token punctuation">,</span> <span class="token string">"save/nets/better_ge.ckpt"</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span> epoch <span class="token keyword">in</span> range<span class="token punctuation">(</span>config<span class="token punctuation">[</span><span class="token string">"epoch"</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">for</span> idx <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> len<span class="token punctuation">(</span>train_hr_imgs<span class="token punctuation">)</span><span class="token punctuation">,</span> batch_size<span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token comment" spellcheck="true"># print(type(train_hr_imgs[idx:idx + batch_size]))</span>
                b_imgs_384 <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>threading_data<span class="token punctuation">(</span>train_hr_imgs<span class="token punctuation">[</span>idx<span class="token punctuation">:</span>idx <span class="token operator">+</span> batch_size<span class="token punctuation">]</span><span class="token punctuation">,</span> fn<span class="token operator">=</span>crop_sub_imgs_fn<span class="token punctuation">,</span>
                                                      is_random<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
                b_imgs_96 <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>threading_data<span class="token punctuation">(</span>b_imgs_384<span class="token punctuation">,</span> fn<span class="token operator">=</span>downsample_fn<span class="token punctuation">)</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'-------------'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'----------'</span><span class="token punctuation">)</span>
                <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">25</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                    loss_D<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>D_loos<span class="token punctuation">,</span> D_OPTIM<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                         feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                             gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> b_imgs_96<span class="token punctuation">,</span>
                                             gan<span class="token punctuation">.</span>y<span class="token punctuation">:</span> b_imgs_384
                                         <span class="token punctuation">}</span><span class="token punctuation">)</span>
                    loss_G<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>G_loos<span class="token punctuation">,</span> G_OPTIM<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                         feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                             gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> b_imgs_96<span class="token punctuation">,</span>
                                             gan<span class="token punctuation">.</span>y<span class="token punctuation">:</span> b_imgs_384
                                         <span class="token punctuation">}</span><span class="token punctuation">)</span>
                    <span class="token keyword">print</span><span class="token punctuation">(</span>loss_D<span class="token punctuation">,</span> loss_G<span class="token punctuation">)</span>
                <span class="token keyword">if</span> idx <span class="token operator">%</span> <span class="token number">20</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
                    saver<span class="token punctuation">.</span>save<span class="token punctuation">(</span>sess<span class="token punctuation">,</span> <span class="token string">"./save/nets/better_all_"</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"_"</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'.ckpt'</span><span class="token punctuation">)</span>

                    _imgs <span class="token operator">=</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>asanyarray<span class="token punctuation">(</span>test_img<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">.</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
                    _imgs <span class="token operator">=</span> _imgs<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span>
                    result_fake_y <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>test_fake_y<span class="token punctuation">]</span><span class="token punctuation">,</span> feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                        gan<span class="token punctuation">.</span>test_x<span class="token punctuation">:</span> _imgs
                    <span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 生成的假照片</span>
                    <span class="token comment" spellcheck="true"># result=sess.run(result_fake_y)</span>
                    strpath <span class="token operator">=</span> <span class="token string">'./preImg/result_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_1.npy'</span>
                    np<span class="token punctuation">.</span>save<span class="token punctuation">(</span>strpath<span class="token punctuation">,</span> result_fake_y<span class="token punctuation">)</span>

                    _imgs2 <span class="token operator">=</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>asanyarray<span class="token punctuation">(</span>test_img<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">.</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
                    _imgs2 <span class="token operator">=</span> _imgs2<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span>
                    result_fake_y <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>test_fake_y<span class="token punctuation">]</span><span class="token punctuation">,</span> feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                        gan<span class="token punctuation">.</span>test_x<span class="token punctuation">:</span> _imgs2
                    <span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 生成的假照片</span>
                    <span class="token comment" spellcheck="true"># result=sess.run(result_fake_y)</span>
                    strpath <span class="token operator">=</span> <span class="token string">'./preImg/result_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_2.npy'</span>
                    np<span class="token punctuation">.</span>save<span class="token punctuation">(</span>strpath<span class="token punctuation">,</span> result_fake_y<span class="token punctuation">)</span>
                    <span class="token comment" spellcheck="true"># print(type(result_fake_y))</span>
    <span class="token keyword">elif</span> type <span class="token operator">==</span> <span class="token string">'pre'</span><span class="token punctuation">:</span>
        saver<span class="token punctuation">.</span>restore<span class="token punctuation">(</span>sess<span class="token punctuation">,</span> <span class="token string">"save/nets/better_all_1_28.ckpt"</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'---------------------恢复训练好的模型，开始预测-----------------------'</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span> num <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            _imgs <span class="token operator">=</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>asanyarray<span class="token punctuation">(</span>test_img<span class="token punctuation">[</span>num<span class="token punctuation">:</span><span class="token punctuation">(</span>num <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">.</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>_imgs<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
            _imgs <span class="token operator">=</span> _imgs<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span>
            <span class="token comment" spellcheck="true"># time.sleep(1)</span>
            result_fake_y <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>test_fake_y<span class="token punctuation">]</span><span class="token punctuation">,</span> feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                gan<span class="token punctuation">.</span>test_x<span class="token punctuation">:</span> _imgs
            <span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 生成的假照片</span>
            strpath <span class="token operator">=</span> <span class="token string">'./preImg/pre_result_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>num<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'.npy'</span>
            np<span class="token punctuation">.</span>save<span class="token punctuation">(</span>strpath<span class="token punctuation">,</span> result_fake_y<span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'ok'</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span>init<span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'---------------------开始新的训练-----------------------'</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span> epoch <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">for</span> idx <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> len<span class="token punctuation">(</span>train_hr_imgs<span class="token punctuation">)</span><span class="token punctuation">,</span> batch_size<span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token comment" spellcheck="true"># print(type(train_hr_imgs[idx:idx + batch_size]))</span>
                b_imgs_384 <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>threading_data<span class="token punctuation">(</span>train_hr_imgs<span class="token punctuation">[</span>idx<span class="token punctuation">:</span>idx <span class="token operator">+</span> batch_size<span class="token punctuation">]</span><span class="token punctuation">,</span> fn<span class="token operator">=</span>crop_sub_imgs_fn<span class="token punctuation">,</span>
                                                      is_random<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
                b_imgs_96 <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>threading_data<span class="token punctuation">(</span>b_imgs_384<span class="token punctuation">,</span> fn<span class="token operator">=</span>downsample_fn<span class="token punctuation">)</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'-------------pre_generator:'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'----------------'</span><span class="token punctuation">)</span>
                <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">25</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                    init_mse_loss<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>init_mse_loss<span class="token punctuation">,</span> G_OPTIM_init<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                                feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                                    gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> b_imgs_96<span class="token punctuation">,</span>
                                                    gan<span class="token punctuation">.</span>y<span class="token punctuation">:</span> b_imgs_384
                                                <span class="token punctuation">}</span><span class="token punctuation">)</span>
                    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'init_mse_loss:'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>init_mse_loss<span class="token punctuation">)</span><span class="token punctuation">)</span>
        saver<span class="token punctuation">.</span>save<span class="token punctuation">(</span>sess<span class="token punctuation">,</span> <span class="token string">"save/nets/cnn_mnist_basic_generator.ckpt"</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span> epoch <span class="token keyword">in</span> range<span class="token punctuation">(</span>config<span class="token punctuation">[</span><span class="token string">"epoch"</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">for</span> idx <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> len<span class="token punctuation">(</span>train_hr_imgs<span class="token punctuation">)</span><span class="token punctuation">,</span> batch_size<span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token comment" spellcheck="true"># print(type(train_hr_imgs[idx:idx + batch_size]))</span>
                b_imgs_384 <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>threading_data<span class="token punctuation">(</span>train_hr_imgs<span class="token punctuation">[</span>idx<span class="token punctuation">:</span>idx <span class="token operator">+</span> batch_size<span class="token punctuation">]</span><span class="token punctuation">,</span> fn<span class="token operator">=</span>crop_sub_imgs_fn<span class="token punctuation">,</span>
                                                      is_random<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
                b_imgs_96 <span class="token operator">=</span> tl<span class="token punctuation">.</span>prepro<span class="token punctuation">.</span>threading_data<span class="token punctuation">(</span>b_imgs_384<span class="token punctuation">,</span> fn<span class="token operator">=</span>downsample_fn<span class="token punctuation">)</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'-------------'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'----------'</span><span class="token punctuation">)</span>
                <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">25</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                    loss_D<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>D_loos<span class="token punctuation">,</span> D_OPTIM<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                         feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                             gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> b_imgs_96<span class="token punctuation">,</span>
                                             gan<span class="token punctuation">.</span>y<span class="token punctuation">:</span> b_imgs_384
                                         <span class="token punctuation">}</span><span class="token punctuation">)</span>
                    loss_G<span class="token punctuation">,</span> _ <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>G_loos<span class="token punctuation">,</span> G_OPTIM<span class="token punctuation">]</span><span class="token punctuation">,</span>
                                         feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                                             gan<span class="token punctuation">.</span>x<span class="token punctuation">:</span> b_imgs_96<span class="token punctuation">,</span>
                                             gan<span class="token punctuation">.</span>y<span class="token punctuation">:</span> b_imgs_384
                                         <span class="token punctuation">}</span><span class="token punctuation">)</span>
                    <span class="token keyword">print</span><span class="token punctuation">(</span>loss_D<span class="token punctuation">,</span> loss_G<span class="token punctuation">)</span>
                <span class="token keyword">if</span> idx <span class="token operator">%</span> <span class="token number">20</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
                    _imgs <span class="token operator">=</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>asanyarray<span class="token punctuation">(</span>test_img<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">.</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
                    _imgs <span class="token operator">=</span> _imgs<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span>
                    result_fake_y <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>test_fake_y<span class="token punctuation">]</span><span class="token punctuation">,</span> feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                        gan<span class="token punctuation">.</span>test_x<span class="token punctuation">:</span> _imgs
                    <span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 生成的假照片</span>
                    <span class="token comment" spellcheck="true"># result=sess.run(result_fake_y)</span>
                    strpath <span class="token operator">=</span> <span class="token string">'./preImg/result_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_1.npy'</span>
                    np<span class="token punctuation">.</span>save<span class="token punctuation">(</span>strpath<span class="token punctuation">,</span> result_fake_y<span class="token punctuation">)</span>

                    _imgs2 <span class="token operator">=</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>asanyarray<span class="token punctuation">(</span>test_img<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">.</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
                    _imgs2 <span class="token operator">=</span> _imgs2<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span>
                    result_fake_y <span class="token operator">=</span> sess<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">[</span>gan<span class="token punctuation">.</span>test_fake_y<span class="token punctuation">]</span><span class="token punctuation">,</span> feed_dict<span class="token operator">=</span><span class="token punctuation">{</span>
                        gan<span class="token punctuation">.</span>test_x<span class="token punctuation">:</span> _imgs2
                    <span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 生成的假照片</span>
                    <span class="token comment" spellcheck="true"># result=sess.run(result_fake_y)</span>
                    strpath <span class="token operator">=</span> <span class="token string">'./preImg/result_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'_2.npy'</span>
                    np<span class="token punctuation">.</span>save<span class="token punctuation">(</span>strpath<span class="token punctuation">,</span> result_fake_y<span class="token punctuation">)</span>
                    saver<span class="token punctuation">.</span>save<span class="token punctuation">(</span>sess<span class="token punctuation">,</span> <span class="token string">"save/nets/ckpt-"</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>epoch<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'-'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>idx<span class="token punctuation">)</span><span class="token punctuation">)</span>
                    <span class="token comment" spellcheck="true"># print(type(result_fake_y))</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="查看效果的工具函数"><a href="#查看效果的工具函数" class="headerlink" title="查看效果的工具函数"></a>查看效果的工具函数</h2><p>将numpy矩阵转换为图片</p>
<pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">from</span> PIL <span class="token keyword">import</span> Image

npz <span class="token operator">=</span> np<span class="token punctuation">.</span>load<span class="token punctuation">(</span><span class="token string">'../preImg/pre_result_5.npy'</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'latin1'</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>npz<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
data <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>npz<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">.</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>

new_im <span class="token operator">=</span> Image<span class="token punctuation">.</span>fromarray<span class="token punctuation">(</span>data<span class="token punctuation">.</span>astype<span class="token punctuation">(</span>np<span class="token punctuation">.</span>uint8<span class="token punctuation">)</span><span class="token punctuation">)</span>
new_im<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
new_im<span class="token punctuation">.</span>save<span class="token punctuation">(</span><span class="token string">'result.png'</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<script>
        document.querySelectorAll('.github-emoji')
          .forEach(el => {
            if (!el.dataset.src) { return; }
            const img = document.createElement('img');
            img.style = 'display:none !important;';
            img.src = el.dataset.src;
            img.addEventListener('error', () => {
              img.remove();
              el.style.color = 'inherit';
              el.style.backgroundImage = 'none';
              el.style.background = 'none';
            });
            img.addEventListener('load', () => {
              img.remove();
            });
            document.body.appendChild(img);
          });
      </script>
            </div>
            <hr/>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力!</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.bmp" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.bmp" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fa fa-user">
                        作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://sunhwee.com" rel="external nofollow noreferrer">洪卫</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fa fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://sunhwee.com/posts/de58ab60.html">https://sunhwee.com/posts/de58ab60.html</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fa fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="https://sunhwee.com" target="_blank">洪卫</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <!-- 
            <div class="reprint1">
                <p>
                    <span class="reprint1-tip">
                        <i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;版权声明:
                    </span>
                    <a href="https://sunhwee.com" class="b-link-green">洪卫の博客</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/posts/de58ab60.html" class="b-link-green">GAN生成对抗网络Tutorial</a>
                </p>
            </div> -->
            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/深度学习/">
                                    <span class="chip bg-color">深度学习</span>
                                </a>
                            
                                <a href="/tags/CV/">
                                    <span class="chip bg-color">CV</span>
                                </a>
                            
                                <a href="/tags/GAN/">
                                    <span class="chip bg-color">GAN</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <!-- <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css"> -->
<!-- <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div> -->

<!-- <script src="/libs/share/js/social-share.min.js"></script> -->
<!-- <script src="/libs/share/js/social-share.min.js"></script> -->

<link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
        </div>
    </div>

    
        <link rel="stylesheet" href="/libs/gitalk/gitalk.css">
<link rel="stylesheet" href="/css/my-gitalk.css">

<div class="card gitalk-card" data-aos="fade-up">
    <div id="gitalk-container" class="card-content"></div>
</div>

<script src="/libs/gitalk/gitalk.min.js"></script>
<script>
    let gitalk = new Gitalk({
        clientID: 'ffe0ea359d9892dfbadb',
        clientSecret: '765e3f453b6537e292b3f4132db5bda05d18bffc',
        repo: 'shw2018.github.io',
        owner: 'shw2018',
        admin: ["shw2018"],
        id: 'posts/de58ab60.html',
        distractionFreeMode: false  // Facebook-like distraction free mode
    });

    gitalk.render('gitalk-container');
</script>
    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }
    /* valine 评论框增加背景图片 */
    #vcomments textarea {
        box-sizing: border-box;
        background: url("/medias/comment_bg.png") 100% 100% no-repeat;
    }

    #vcomments input[type=text],
    #vcomments input[type=email],
    #vcomments input[type=url],
    #vcomments textarea {
        box-sizing: border-box;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #42b983;
        font-weight: 500;
        /* text-decoration: underline; */
        text-decoration: none;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    /* 修复评论第一行位置错位 */
    .v .vlist .vcard {
    padding-top: 2.5em !important ;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div id="vcomments" class="card-content"></div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<!-- <link rel="stylesheet" type="text/css" href="/libs//libs/jQuery-emoji/dist/css/jquery.emoji.css">
<script src="/libs//libs/jQuery-emoji/dist/js/jquery.emoji.min.js"></script>
<script src="/libs//libs/jQuery-emoji/dist/js/emoji.list.js"></script> -->
<script>
    new Valine({
        el: '#vcomments',
        appId: 'lLPMWOb3ctIegVovkXFVQHVz-gzGzoHsz',
        appKey: 'rR3LIiSBOYukuM3gLpL76rQt',
        notify: 'true' === 'true',
        verify: 'true' === 'true',
        visitor: 'true' === 'true',
        avatar: 'https://s.gravatar.com/avatar/0007991f99268c04f1aa4cdd9bea93b4?s=80',
        pageSize: '10',
        lang: 'zh-cn',
        placeholder: '没有Github账号的在这里留言评论～',

    });

//     function parse_emoji() {
//     jQuery(".vcontent").emojiParse({
//       basePath: '/libs/jQuery-emoji/images/emoji',
//       icons: emojiLists   // 注：详见 js/emoji.list.js
//     });
//   }
  
//   setTimeout(function() {
//     // jQuery emoji 解析
//     parse_emoji();
//     // jquery emoji 初始化
//     jQuery(".veditor").emoji({
//       showTab: true,
//       animation: 'slide',
//       basePath: '/libs/jQuery-emoji/images/emoji',
//       icons: emojiLists  // 注：详见 js/emoji.list.js
//     });
//     jQuery(".vmore").on("click", function() {
//       setTimeout(function() {
//         parse_emoji();
//       }, 500);
//     });
//   }, 800)

</script>
    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fa fa-dot-circle-o"></i>&nbsp;本篇
            </div>
            <div class="card">
                <a href="/posts/de58ab60.html">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/22.jpg" class="responsive-img" alt="GAN生成对抗网络Tutorial">
                        
                        <span class="card-title">GAN生成对抗网络Tutorial</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
本文转载于博客园，感觉写得比较清晰，保存一下供以后查看。

GAN生成式对抗网络（一）——原理生成式对抗网络（GAN, Generative Adversarial Networks ）是一种深度学习模型
GAN包括两个核心模块
1.生成
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2019-12-10
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Reaserch/" class="post-category" target="_blank">
                                    Reaserch
                                </a>
                            
                            
                        </span>
                    </div>
                </div>

                
                <div class="card-action article-tags">
                    
                    <a href="/tags/深度学习/" target="_blank">
                        <span class="chip bg-color">深度学习</span>
                    </a>
                    
                    <a href="/tags/CV/" target="_blank">
                        <span class="chip bg-color">CV</span>
                    </a>
                    
                    <a href="/tags/GAN/" target="_blank">
                        <span class="chip bg-color">GAN</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fa fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/posts/14089fc4.html">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/5.jpg" class="responsive-img" alt="研究生新手刚开始应该怎么看英文文献？">
                        
                        <span class="card-title">研究生新手刚开始应该怎么看英文文献？</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
本文原文转载于知乎，感觉总结得不错。

前言千万！千万！不要从头到尾按顺序看！
强烈推荐美国公立常青藤明尼苏达大学Peter W. Carr教授传授的阅读顺序
明尼苏达大学是世界著名公立研究型大学，在2019年USNews世界大学排名中位
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2019-12-09
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Reaserch/" class="post-category" target="_blank">
                                    Reaserch
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/深度学习/" target="_blank">
                        <span class="chip bg-color">深度学习</span>
                    </a>
                    
                    <a href="/tags/Paper-Reading/" target="_blank">
                        <span class="chip bg-color">Paper Reading</span>
                    </a>
                    
                    <a href="/tags/CV/" target="_blank">
                        <span class="chip bg-color">CV</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: 洪卫の博客<br />'
            + '作者: 洪卫<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- shw2018 洪卫 add 2019.10.15 -->
<!-- 代码块功能依赖 -->

    <script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>
    
    
    <!-- 代码语言 -->
    
    <script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>
    
        
    <!-- 代码块复制 -->
    
    <script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>
    <script type="text/javascript" src="/libs/codeBlock/clipboard.min.js"></script>
    
    
    <!-- 代码块收缩 -->
    
    <script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>
    
    
    <!-- 代码块折行 -->
    
    <style type="text/css">
    code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
    </style>
    
    
    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <!-- <a class="btn-floating btn-large bg-color"> -->
    <a class="btn-floating btn-large ">    <!-- 去掉按键 bg-color  洪卫 shw2018 modify 2019.09.13-->
        <i class="fa fa-list"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h1, h2, h3, h4, h5'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).slideUp(500);
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).slideDown(500);
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>
    

</main>


        <footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            Copyright&copy; 2019 Hongwei. <span class="with-love" id="heart"><i class="fa fa-heart" style="color:#ff71a8"></i> </span> 渝ICP备19012360号

            <br>
            <span id="sitetime"></span><span class="my-face">ღゝ◡╹)ノ♡</span>
            <br>

            

            
                    
                        <span id="busuanzi_container_site_pv" style='display:none'></span>
                        总访问量: <span id="busuanzi_value_site_pv" class="white-color"></span>
                    
        
                    
                        <span id="busuanzi_container_site_uv" style='display:none'></span>
                        人次&nbsp; | &nbsp;访客人数: <span id="busuanzi_value_site_uv" class="white-color"></span> 人
                    
    
            

            
                &nbsp; | &nbsp;字数统计:&nbsp;
                <span class="white-color">75.7k</span> 字
            

            
            <br>

            <!-- 友盟站点统计 洪卫 shw2018 add 2019.09.12  -->
            <script type="text/javascript">
                var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
                document.write(unescape("%3Cspan id='cnzz_stat_icon_1278019489'%3E%3C/span%3E%3Cscript src='" +
                    cnzz_protocol +
                    "s9.cnzz.com/z_stat.php%3Fid%3D1278019489%26online%3D1%26show%3Dline' type='text/javascript'%3E%3C/script%3E"
                    ));
            </script>

        </div>

        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/shw2018" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fa fa-github"></i>
    </a>


 
    <a href="https://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=948840751@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fa fa-envelope-open"></i>
    </a>




    <a href="https://zhihu.com/people/hongwei-60-34" class="tooltipped" target="_blank" data-tooltip="访问我的知乎" data-position="top" data-delay="50">
        <i class="fa fa-inverse">知</i>
    </a>



    <a href="http://wpa.qq.com/msgrd?v=3&uin=948840751&site=qq&menu=yes" class="tooltipped" target="_blank" data-tooltip="访问我的QQ空间" data-position="top" data-delay="50">
        <i class="fa fa-qq"></i>
    </a>



    <a href="https://weibo.com/3010027571" class="tooltipped" target="_blank" data-tooltip="关注我的微博" data-position="top" data-delay="50">
        <i class="fa fa-weibo"></i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fa fa-rss"></i>
    </a>
</div>

    </div>
</footer>

<div class="progress-bar"></div>

<!-- 不蒜子计数初始值纠正 -->
<script>
    $(document).ready(function () {

        var int = setInterval(fixCount, 50);
        var pvcountOffset = 10000;
        var uvcountOffset = 4000;

        function fixCount() {
            if (document.getElementById("busuanzi_container_site_pv").style.display != "none") {
                $("#busuanzi_value_site_pv").html(parseInt($("#busuanzi_value_site_pv").html()) +
                    pvcountOffset);
                clearInterval(int);
            }
            if ($("#busuanzi_container_site_pv").css("display") != "none") {
                $("#busuanzi_value_site_uv").html(parseInt($("#busuanzi_value_site_uv").html()) +
                    uvcountOffset); // 加上初始数据 
                clearInterval(int);
            }
        }
    });
</script>

<script language=javascript>
    function siteTime() {
        window.setTimeout("siteTime()", 1000);
        var seconds = 1000;
        var minutes = seconds * 60;
        var hours = minutes * 60;
        var days = hours * 24;
        var years = days * 365;
        var today = new Date();
        var todayYear = today.getFullYear();
        var todayMonth = today.getMonth() + 1;
        var todayDate = today.getDate();
        var todayHour = today.getHours();
        var todayMinute = today.getMinutes();
        var todaySecond = today.getSeconds();

        /* Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
        year - 作为date对象的年份，为4位年份值
        month - 0-11之间的整数，做为date对象的月份
        day - 1-31之间的整数，做为date对象的天数
        hours - 0(午夜24点)-23之间的整数，做为date对象的小时数
        minutes - 0-59之间的整数，做为date对象的分钟数
        seconds - 0-59之间的整数，做为date对象的秒数
        microseconds - 0-999之间的整数，做为date对象的毫秒数 */

        var t1 = Date.UTC(2019, 08, 10, 00, 00, 00); //北京时间2019-8-1 00:00:00
        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
        var diff = t2 - t1;
        var diffYears = Math.floor(diff / years);
        var diffDays = Math.floor((diff / days) - diffYears * 365);
        var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
        var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) / minutes);
        var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours - diffMinutes *
            minutes) / seconds);
        document.getElementById("sitetime").innerHTML = "本站已勉强运行 " + diffYears + " 年 " + diffDays + " 天 " + diffHours +
            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒" ;
    } /*因为建站时间还没有一年，就将之注释掉了。需要的可以取消*/
    siteTime();
</script>

<!-- 鼠标点击烟花爆炸效果  洪卫 shw2018 add 2019.09.09 -->


<!-- 在线聊天工具  洪卫 shw2018 add 2019.09.11 -->
<!-- <script src="//code.tidio.co/rwjwdlfhkt9pp7zjsjcva0ug96bvfhss.js"></script> -->

<!-- 代码高亮  洪卫 shw2018 add 2019.09.11 -->
<!-- <script src="/libs/highlight/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script> -->



        <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fa fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>
<!-- 洪卫 shw2018 modify 2019.08.30 -->
<script src="/js/search.js"></script>
<!-- <script src="/js/search.js"></script> -->
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
        <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


        <script src="/libs/materialize/materialize.min.js"></script>
        <script src="/libs/masonry/masonry.pkgd.min.js"></script>
        <script src="/libs/aos/aos.js"></script>
        <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
        <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
        <script src="/js/matery.js"></script>

        <!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-146860938-1"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag() {
        dataLayer.push(arguments);
    }

    gtag('js', new Date());
    gtag('config', 'UA-146860938-1');
</script>



        
            <script src="/libs/others/clicklove.js"></script>
        

        
            <script async src="/libs/others/busuanzi.pure.mini.js"></script>
        

        <!-- 洪卫 shw2018 add 2019.08.28 -->
        <script type="text/javascript">
            var OriginTitile = document.title,
                st;
            document.addEventListener("visibilitychange", function () {
                document.hidden ? (document.title = "看不见我🙈~看不见我🙈~", clearTimeout(st)) : (document.title =
                    "(๑•̀ㅂ•́) ✧被发现了～", st = setTimeout(function () {
                        document.title = OriginTitile
                    }, 3e3))
            })
        </script>

        <!-- 天气接口控件  洪卫 shw2018 add 2019.09.09 -->
        
            <!-- 天气接口插件  洪卫 shw2018 add 2019.09.09 -->
<style type="text/css">
  #weather-float-he {
    position: absolute;
    z-index: 9999;
    padding: 0px;
    top: 8px;
    left: 8px;
  }
</style>

<script type="text/javascript">
  WIDGET = {
    FID: '1tFpFZ5Mtj'
  }
</script>
<!-- <script type="text/javascript" src="https://apip.weatherdt.com/float/static/js/r.js?v=1111"></script> -->

<script type="text/javascript">
  //只在桌面版网页启用特效
  var windowWidth = $(window).width();
  if (windowWidth > 768) {
    document.write('<script type="text/javascript" src="https://apip.weatherdt.com/float/static/js/r.js?v=1111"><\/script>');
  }
</script>
        

        <!-- 鼠标点击烟花爆炸效果  洪卫 shw2018 add 2019.09.09 -->
        

        <!-- 背景雪花飘落特效洪卫 shw2018 add 2019.09.10 -->
        
            <script type="text/javascript">
            //只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/js/sakura.js"><\/script>');
            }
            </script>
        

        <!-- 鼠标点击文字特效 洪卫 shw2018 add 2019.09.10-->
        

        <!-- 背景雪花飘落特效 洪卫 shw2018 add 2019.09.10 -->
        

        <!-- 在线聊天工具  洪卫 shw2018 add 2019.09.11 -->
        
            <script src="//code.tidio.co/rwjwdlfhkt9pp7zjsjcva0ug96bvfhss.js"></script>
            <!--  在线聊天位置自定义  洪卫 shw2018 add 2019.09.13  -->
            <script> 
                $(document).ready(function () {

                    setInterval(change_Tidio, 50);  
                    function change_Tidio() { 

                        var tidio=$("#tidio-chat iframe");
                        if(tidio.css("display")=="block"&& $(window).width()>977 ){
                            document.getElementById("tidio-chat-iframe").style.bottom= ($("div#backTop.top-scroll").css("display")=="none" &&$(window).width()>977)>0? "-40px" : ($("div.toc-title").length&&$(window).width()>977)>0?"80px":"20px";   
                            document.getElementById("tidio-chat-iframe").style.right="-15px";   
                            document.getElementById("tidio-chat-iframe").style.height=parseInt(tidio.css("height"))>=520?"520px":tidio.css("height");
                            document.getElementById("tidio-chat-iframe").style.zIndex="997";
                        } 
                        else if(tidio.css("display")=="block"&&$(window).width()>601 &&$(window).width()<992 ){
                            document.getElementById("tidio-chat-iframe").style.bottom= ($("div#backTop.top-scroll").css("display")=="none" && 601< $(window).width()<992)>0? "-40px":"20px" ;   
                            document.getElementById("tidio-chat-iframe").style.right="-15px"; 
                            document.getElementById("tidio-chat-iframe").style.zIndex="997";
                        }
                        else if(tidio.css("display")=="block"&&$(window).width()<601 && parseInt(tidio.css("height"))<230){
                            document.getElementById("tidio-chat-iframe").style.bottom= ($("div#backTop.top-scroll").css("display")=="none" && $(window).width()<601)>0? "-10px":"45px" ;   
                            document.getElementById("tidio-chat-iframe").style.zIndex="997";
                        }

                        if( tidio.css("display")=="block"&&$(window).width()<601 && parseInt(tidio.css("height"))>=230){
                            document.getElementById("tidio-chat-iframe").style.zIndex="998";
                        }
                    } 
                }); 
            </script>
        

        <!--  html 预加载脚本  洪卫 shw2018 add 2019.09.12  -->
        <script src="/js/instant.page.js" type="module"></script>

        <!-- 背景 canvas-nest  洪卫 shw 2018  add 2019.09.15-->
        

        <!-- 背景静止彩带  洪卫 shw 2018  add 2019.09.15-->
        

        <!-- 背景动态彩带 洪卫 shw 2018  add 2019.09.15-->
        
            <script type="text/javascript">
            var windowWidth = $(window).width();
            if (windowWidth > 992) {
                document.write('<script type="text/javascript" src="/libs/background/ribbon-dynamic.js"><\/script>');
            }
            </script>
        

        <!-- <script src='https://cdn.jsdelivr.net/gh/fcmam5/nightly.js@v1.0/dist/nightly.min.js'></script> -->

    </body>
</html>